2010-08-16 68 views
25

A(當電池是新創建):我應該如何添加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; 
} 

或B(每次當小區被發現時間):

- (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或B?謝謝!

UPDATE解決方案(感謝您的答案):

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

回答

11

這是關於性能。使用A,您可以重複使用包含所有子視圖的單元格和B,只重用原始單元格,並在每次迭代中添加一個新的子視圖,而恕我直言不如A re:performance。

我說可以創建一個UITableView子類,或者使用的解決方案A.

+0

除了性能,確實乙造成內存泄漏? – ohho 2010-08-16 03:52:21

+0

@ohho,不這麼認爲。一切看起來都很好。 – 2010-08-16 03:56:50

+3

難道B不會給細胞添加標籤嗎?我想在一些滾動後,你最終會得到多個單元格在其子視圖中保存多個標籤。如果我錯了,請糾正我。 – Mike 2010-10-09 16:50:14

11

當您創建單元格中。你應該只添加子視圖,但分配值每次都在標籤等B.

如果你創建你自己的UITableViewCell的子類並添加它自己的子視圖,這個解決方案就會自然而然地出現。

就是這樣。

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

這樣你得到的只有一次分配的子視圖的性能優勢,你可以直接設置在子視圖相應的屬性需要。

相關問題