Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I addSubview to cell.contentView?

A (when the cell is newly created):

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

or B (every time when the cell is found):

- (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 or B? Thanks!

UPDATE Solution (thanks for the answers):

- (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;
}
like image 544
ohho Avatar asked Aug 16 '10 03:08

ohho


2 Answers

It's all about performance. With A, you reuse the cell with all of its subviews, with B, you reuse only the raw cell and add a new subview every iteration, which IMHO is not as good as A re: performance.

I say either create a UITableView subclass or use solution A.

like image 150
Jacob Relkin Avatar answered Nov 10 '22 02:11

Jacob Relkin


You should only add the sub views when you create the cell as in A, however assign the values to the labels etc every time as in B.

This solution would fall out naturally if you create your own subclass of UITableViewCell that adds it's own sub views.

Something like this.

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

This way you get the performance benefits of only allocating the sub views once and you can just set the appropriate properties on the subview as needed.

like image 10
Gary Avatar answered Nov 10 '22 01:11

Gary