0

// 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.

avi
  • 57
  • 1
  • 7

3 Answers3

0

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.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • Thanks for you response; I thought about it, but then didn't want to go that route because this would mean that I create _n_ copies of pretty much the same cell with slight variations. The bad thing about this is making slight adjustment to the views would then become cumbersome as I would have to make the change in multiple copies. I now have a separate nib for iPhone and iPad and this itself is driving me nuts when I need to make a small change to a view that is available in both versions. – avi May 11 '12 at 20:39
  • Okay, but your question was about passing the cell identifier to the nib loading code. That's what this does.... – lnafziger May 12 '12 at 04:21
  • Sorry if it wasn't obvious,my question was more towards passing a cell identifier to loadNibNamed function so that I can dynamically create cells with different identifiers and not have to duplicate cells and create multiple copies of the cell objects every time a nib file is loaded. – avi May 15 '12 at 18:44
  • Rather than putting them all in different nib's, you should just put them in one nib. It won't actually load them until you call dequeueReusableCellWithIdentifier: so you actually create extra overhead when you load multiple nib's. – lnafziger May 15 '12 at 20:06
0

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;
}
user500
  • 4,519
  • 6
  • 43
  • 56
-1

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.

jrturton
  • 118,105
  • 32
  • 252
  • 268