4

I am trying to have custom TableViewCell with initWithStyle, since it says initWithFrame is deprecated after 3.0. Everything worked fine with initWithFrame before.

Is there any tutorials or sample code available for this? Thanks.

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
0pcl
  • 1,154
  • 4
  • 17
  • 22

1 Answers1

7

I have subclassed UITableViewCell then override the initWithStyle method.

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        // Initialization code
        msgText = [[UILabel alloc] init];
        [self.contentView addSubview:msgText];  
    }
    return self;
}

msgText is a UILabel property of the class and I set the text property of the label elsewhere. You can add any views to self.contentView you like. I also set the frame of each of the subviews when I add the content like text and/or images.

acqu13sce
  • 3,789
  • 4
  • 25
  • 32
  • Can you still define positions of the elements in layoutSubviews? tnx – Mike Bevz Jan 23 '12 at 23:35
  • 1
    Absolutely. Set the frame of the element you're adding to the UITableViewCell subview relative to the top-left of the UITableViewCell. – acqu13sce Jan 24 '12 at 03:58
  • 1
    The initWithStyle method doesn't get called for me. What do you do in the tableView's cellForRowAtIndexPath? – Michael Oct 16 '12 at 08:10