Rooban Ponraj A
Rooban Ponraj A

Reputation: 281

loadNibNamed at Base Class override the instance of Child Class

I have class hierachy with

ParentCell extends UITableViewCell
ChildCell extends ParentCell

ParentCell have separate XIB, In child cell i was creating and adding only one button to one view at ParentCell XIB. but i cant add action for this button. Because even i was creating a instance for ChildCell, that returns the instance of ParentCell

Because i use loadNibNamed to get the XIB with IBOutlet connections. @ initWithStyle method in ParentCell Class

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self = [[NSBundle mainBundle] loadNibNamed:@"ParentCell" owner:self options:nil]
               [0];
    }
    return self;
}

@ initWithStyle method in ChildCell Class

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.button=[[UIButton alloc] init];
        [self.contentView addSubview:button];
    }
    return self;
}

@ View Controller

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
            static NSString *CellIdentifier = @"ChildCell ";
            ChildCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil)
            {
              cell=[[ChildCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:CellIdentifier];

                NSLog(@"Cell : %@", cell);  //this have instance of ParentCell instead of ChildCell
            }
}

Now temporarily solved by this way

@ initWithStyle method in ParentCell Class

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSBundle *mainBundle = [NSBundle mainBundle];
        NSArray *views = [mainBundle loadNibNamed:@"ParentCell"
                                            owner:self
                                          options:nil];
       //Here we are linking the view with appropriate IBOutlet by their tag
       self.lblTitle=[views[0] viewWithTag:100];
       self.lblContent=[views[0] viewWithTag:200];
    }
    return self;
}

@ initWithStyle method in ChildCell Class

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.button=[[UIButton alloc] init];
        [self.contentView addSubview:button];
    }
    return self;
}

But i don't know this is right approach to follow, or we have some other better approach then this..

Upvotes: 0

Views: 485

Answers (1)

Dávid Kaszás
Dávid Kaszás

Reputation: 1977

You should use the registerNib:forCellReuseIdentifier: method at the viewDidLoad of you UITableView class.

static NSString *parentCellIdentifier = @"parentCellIdentifier";
static NSString *childCellIdentifier = @"childCellIdentifier";

[self.tableView registerNib:[UINib nibWithNibName:@"ParentCell" bundle:nil] forCellReuseIdentifier:parentCellIdentifier];
[self.tableView registerNib:[UINib nibWithNibName:@"ChildCell" bundle:nil] forCellReuseIdentifier:childCellIdentifier];

(Don't forget to set the appropriate ReuseIdentifier at the XIB file)

This is the best practice, and you can get rid of your initWithStyle implementations.

Upvotes: 1

Related Questions