2010-08-16 8 views
25

A (gdy komórka jest nowo utworzone):Jak powinienem dodaćAdarview do 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; 
} 

lub B (za każdym razem, gdy komórka jest znaleźć):

- (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 lub B? Dzięki!

UPDATE Solution (dzięki za odpowiedzi):

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

Odpowiedz

11

Wszystko zależy od wydajności. Za pomocą A ponownie wykorzystujesz komórkę wraz ze wszystkimi jej subskrybcjami, przy pomocy B, ponownie używasz tylko surowej komórki i dodajesz nową subskrypcję do każdej iteracji, której IMHO nie jest tak dobra, jak A: wydajność.

mówię albo stworzyć UITableView podklasy lub użycia roztwór A.

+0

Oprócz wydajności, czy B przyczyną wycieku pamięci? – ohho

+0

@ohho, nie sądzę. Wszystko mi dobrze wygląda. –

+3

Czy B nie będzie dodawać etykiet do komórki? Myślę, że po pewnym przewijaniu skończysz z wieloma komórkami mającymi wiele etykiet w swoich podstronach. Proszę popraw mnie jeżeli się mylę. – Mike

11

należy dodać tylko sub widoki podczas tworzenia komórek jak w jednak przypisać wartości do etykiet itp każdym razem jak w B.

Rozwiązanie to wypadałoby naturalnie, jeśli utworzysz własną podklasę UITableViewCell, która dodaje własne widoki podrzędne.

Coś takiego.

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

W ten sposób można uzyskać korzyści wydajności tylko przydzielania sub widoki raz i można po prostu ustawić odpowiednie właściwości na podrzędny, ile potrzeba.

Powiązane problemy