views:

41

answers:

1

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;
+3  A: 

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.

Jergason