2010-08-16 5 views
25

A (lorsque la cellule est nouvellement créée):Comment devrais-je ajouter Subview à cell.contentView?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     CGRect frame = CGRectMake(0, 0, 160, 50); 
     UILabel *label = [[UILabel alloc] initWithFrame:frame]; 
     label.textAlignment = UITextAlignmentRight; 
     label.text = @"9:00am"; 
     [cell.contentView addSubview:label]; 
     [label release]; 
    } 

    return cell; 
} 

ou B (chaque fois que la cellule se trouve):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
    } 

    CGRect frame = CGRectMake(0, 0, 160, 50); 
    UILabel *label = [[UILabel alloc] initWithFrame:frame]; 
    label.textAlignment = UITextAlignmentRight; 
    label.text = @"9:00am"; 
    [cell.contentView addSubview:label]; 
    [label release]; 

    return cell; 
} 

A ou B? Merci!

MISE À JOUR Solution (merci pour les réponses):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell";  
    UILabel *label; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     CGRect frame = CGRectMake(0, 0, 160, 50); 
     label = [[UILabel alloc] initWithFrame:frame]; 
     label.textAlignment = UITextAlignmentRight; 
     label.tag = 1; 
     [cell.contentView addSubview:label]; 
     [label release]; 
    } else { 
     label = (UILabel *) [cell viewWithTag:1]; 
    } 

    label.text = [NSString stringWithFormat:@"%d", [indexPath row]]; 

    return cell; 
} 

Répondre

11

Tout est une question de performance. Avec A, vous réutilisez la cellule avec toutes ses sous-vues, avec B, vous ne réutilisez que la cellule brute et ajoutez une nouvelle sous-vue à chaque itération, ce qui n'est pas aussi bon que A re: performance.

je dis soit créer une solution sous-classe UITableView ou de l'utilisation A.

+0

Outre la performance, ne B cause de fuite de mémoire? – ohho

+0

@ohho, ne le pense pas. Tout me semble bien. –

+3

B ne continuerait-il pas d'ajouter des étiquettes à la cellule? Je pense qu'après un certain défilement, vous finiriez avec plusieurs cellules contenant plusieurs étiquettes dans leurs sous-vues. Corrigez-moi si j'ai tort, s'il-vous plait. – Mike

11

Vous ne devez ajouter les sous vues lors de la création de la cellule comme dans A, toutefois attribuer des valeurs aux étiquettes, etc chaque fois que dans B.

Cette solution tomberait naturellement si vous créez votre propre sous-classe de UITableViewCell qui ajoute ses propres sous-vues.

Quelque chose comme ça.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     CGRect frame = CGRectMake(0, 0, 160, 50); 
     UILabel *label = [[UILabel alloc] initWithFrame:frame]; 
     label.textAlignment = UITextAlignmentRight; 
     [cell.contentView addSubview:label]; 
     [label release]; 
    } 

    // Get a reference to the label here 

    label.text = @"9:00am"; 

    return cell; 
} 

De cette façon, vous obtenez les avantages de performance de l'attribution des points de vue que sous une fois et vous pouvez simplement définir les propriétés appropriées sur la sous-vue selon les besoins.

Questions connexes