How to load a tableview cell using a different cell identifier than the one specified in the nib file

2.1k views Asked by At

// actual question
I need help in loading the nib file (tableviewcell) using a different cell Identifier than the one specified in the nib.

// background on what I am doing
I have a custom UITableViewCell with few labels, few images, and few buttons. All this is laid out in a nib file. The content of the tableview cell needs to change dynamically based on certain conditions i.e if landscape a button is not displayed; If there is no content to display in a label then the label is not displayed etc. Adjacent label/view on the left should extend to fill up the space of the missing label/button.

I am able to get this to work. The way I did this is in cellForRowAtIndexPath I
- remove the views that doesn't need to be displayed using removeFromSuperView and by
- adjusting the frame and call setNeedsDisplay for the view that needs adjusting.

I use the same cell Identifier mentioned in the nib file to dequeue the cell. If the returned cell is nil then I use the loadNibNamed:withOwner:options to load the nib. However I am afraid that this will cause issues when scrolling back and forth as the cell being reused might not have the views that are required to display content when the conditions aren't met as they might have already been removed.

To fix this issue, I am planning to use a different cell identifier for different conditions that trigger a different look and feel of the cell. Alternatively I am also considering to just hide the view and extend the adjacent view over the hidden view. This way I don't need to deal with different cell identifiers.

Edit2:
My alternative approach of hiding and adjusting frame of adjacent view worked for my needs.

However I still want to find the answer to my actual question I described above.

// potential solution
I was wondering if I could pass the cell identifier to the nib loading code via the options parameter of loadNibNamed function. Is this possible? If you guys can provide a sample code to achieve this that would be awesome.

Thanks for your time.

3

There are 3 answers

4
lnafziger On

All you need to do is create multiple cells in the nib with different identifiers and then call dequeueReusableCellWithIdentifier with the appropriate identifier in order to get a reference to the appropriate type of cell.

0
jrturton On

You can't change the cell reuse identifier from that specified in the nib. Reference. The options dictionary you are talking about won't do this for you either. The options are related to proxy objects in the nib.

Rather than removing subviews, you would be better off simply hiding them. You could have IBOutletCollections to make bulk hiding/unhiding easier. You could reset the cell to its default state in prepareForReuse if needed.

Look at UINib as well - this gives faster creation of objects from a nob than loadNibNamed, which will help your scrolling performance.

0
user500 On

I'm not very proud of this solution and may do problems, but I would try set cell's identifier after loading from nib.

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil].lastObject;
    [self setValue:reuseIdentifier forKeyPath:@"_reuseIdentifier"];

    // ...

    return self;
}