2010-08-16 8 views
25

A (wenn die Zelle neu erstellt wird):Wie soll ich subview zu cell.contentView hinzufügen?

- (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; 
} 

oder B (jedes Mal, wenn die Zelle gefunden wird):

- (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 oder B? Vielen Dank!

UPDATE Solution (danke für die Antworten):

- (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; 
} 

Antwort

11

Es geht um Leistung. Mit A verwenden Sie die Zelle mit all ihren Unteransichten, mit B verwenden Sie nur die Rohzelle und fügen jeder Iteration eine neue Unteransicht hinzu, die IMHO nicht so gut ist wie A re: performance.

ich sagen, entweder erstellen UITableView Unterklasse oder Anwendungslösung A.

+0

Neben Leistung, tut B Ursache Speicherleck? – ohho

+0

@ohho, glaube nicht. Alles sieht gut für mich aus. –

+3

Würde B nicht fortfahren, Etiketten zu der Zelle hinzuzufügen? Ich denke, nach einigem Scrollen würden Sie mehrere Zellen mit mehreren Labels in ihren Unteransichten haben. Bitte korrigieren Sie mich, falls ich falsch liege. – Mike

11

Sie sollten nur die Unteransichten hinzufügen, wenn Sie die Zelle als in A erstellen, jedoch weisen Sie die Werte zu den Etiketten usw. jedes Mal, wie in B.

Diese Lösung würde natürlich ausfallen, wenn Sie Ihre eigene Unterklasse von UITableViewCell erstellen, die eigene Unteransichten hinzufügt.

So etwas wie das.

- (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; 
} 

Auf diese Weise können die Leistungsvorteile von bekommen nur die Teilansichten einmal Aufteilung und Sie können die entsprechenden Eigenschaften auf der subview je nach Bedarf gerade eingestellt haben.

Verwandte Themen