Blazer
Blazer

Reputation: 14277

initWithStyle:(UITableViewCellStyle) not called

A weird thing just happend, i trying to build my custom tablecell but my initWithStyle is not called.

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

My Tablecell looks normal:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        NSLog(@"xx1111");
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

How i'm trying to load the Customcell nib:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *TFDCustomCell = @"TFDCell";
    TFDCell *cell = (TFDCell *)[tableView dequeueReusableCellWithIdentifier:TFDCustomCell];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TFDCell"
                                                     owner:self options:nil];
        for (id oneObject in nib) if ([oneObject isKindOfClass:[TFDCell class]])
            cell = (TFDCell *)oneObject;
    }

return cell;

}

But the NSLog(@"xx1111"); doenst appear in my logs. When i place a NSLog in 'setSelected' it works 'fine'

Upvotes: 3

Views: 3936

Answers (3)

zeeawan
zeeawan

Reputation: 6895

You're not initializing the table's cells with initWithStyle so initWithStyle of your custom cell won't be fired. But awakeFromNib will be called for your initialization call of dequeueReusableCellWithIdentifier.

Upvotes: 0

Blazer
Blazer

Reputation: 14277

The solution was simple

-(id)initWithCoder:(NSCoder *)aDecoder
{
    NSLog(@"initWithCoder");

    self = [super initWithCoder: aDecoder];
    if (self)
    {

    }
    return self;
}

Upvotes: 13

Rostyslav Druzhchenko
Rostyslav Druzhchenko

Reputation: 3773

As I know if you load your view (in current case cell) from nib initWithStyle: method wont be called. Overload awakeFromNib: method instead to make custom initialization.

Upvotes: 5

Related Questions