Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing UITableViewCell correctly?

What is the difference between adding the subview to self and or to the content view?

Subview added to self

- (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) {
    UIImage *img = [UIImage imageNamed:@”lol.jpg”]; 
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img]; 
    [self addSubview:imgView];
    [imgView release]; 
    return self;
}

Subview added to contentView

- (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) {
    UIImage *img = [UIImage imageNamed:@”lol.jpg”]; 
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img]; 
    [self.contentView addSubview:imgView];
    [imgView release]; 
    return self;
}
like image 225
Sheehan Alam Avatar asked Jul 30 '10 04:07

Sheehan Alam


1 Answers

According to the Apple docs:

The content view of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.

Generally you add to a contentView when you are comfortable with your resizing and positioning of your content being handled by the autoresize settings, and subclass UITableViewCell when you need some custom behavior and such. The Apple Table View Programming Guide has a great section on customizing UITableViewCells.

like image 171
jergason Avatar answered Nov 09 '22 22:11

jergason