Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UITableVIewCell initialization not called

I have custom UiTablleviewCell with some images and labels, and I would like to have rotated label in tableview cell...so I would like to edit initWithStyle method, but it seems like it's never called.

- (id)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString*)reuseIdentifier{
NSLog(@"creating cell");
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
}
return self;}

but in my log, I cant see this message. In tableview I have standard cellForRow method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *simpleTableIdentifier = @"messagesCell";
    TBCellMessagesCell *cell = (TBCellMessagesCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // smt stuff

    return cell;
}

so I'm wondering how does tableview initialize tableviewcells, I can think about some workarounds but I would like to have it clean. Thank you.

like image 469
beretis Avatar asked Dec 01 '13 15:12

beretis


2 Answers

If the cells come from a storyboard or nib file, then initWithStyle:reuseIdentifier is not called, initWithCoder: is called instead.

Here's a typical implementation of an overwritten initWithCoder::

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
       // Do your custom initialization here
    }
    return self;
}

Will not work if you need to access IBOutlet during custom initialization.

like image 121
Daniel Martín Avatar answered Dec 03 '22 06:12

Daniel Martín


You might also consider the awakeFromNib method if you need to talk with IBOutlet.

Discussion
The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet and action connections already established.

like image 28
Vincent Bernier Avatar answered Dec 03 '22 07:12

Vincent Bernier