[iPhone SDK] - reuseIdentifier with loadNibNamed

I want to be able to use the reuseIdentifier / dequeueReusableCellWithIdentifier that is normally available with initWithFrame:reuseIdentifier:. Unfortunately, the reuseIdentifier property is read-only. Has anyone got around this problem?

MBP, Mac OS X (10.5.3)

Posted on Aug 14, 2008 11:38 AM

Reply
7 replies

Aug 17, 2008 5:05 PM in response to hilken2

You're thinking about this in the wrong way.
There are 2 pieces to this. As was said above, the first thing is make sure that in the Nib for the Cell you enter the identifier value. This ensures that the instance that's created with the Nib info has the right identifier.
The second piece is that you absolutely do NOT want the identifier to be anything other than a constant. The identifier specifies the pool from which you can take reusable cells and it makes no sense to change it. When you do the following

MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

it tries to pull a cell from the pool specified by MyIdentifier. That's it. If there's no previously cell tagged for reuse, it returns nil and then you recreate one using the following code:

if (cell == nil) {
NSArray * MyCustomCellNib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil];
cell = (MyCustomCell *)[MyCustomCellNib objectAtIndex:1];
}

(which assumes you've created a nib called MyCustomCell.nib and the only thing in it apart from First Responder and File's Owner is your custom tableview cell)

This code does work, and does reuse cells properly.

Aug 17, 2008 5:26 PM in response to H Asseily

H Asseily,

Perhaps you are working with SDK 2.1? In 2.0 I have no problems with reuse of table cells created in code. Table cells loaded from nib files however are not being reused. I have tried setting the file's owner to NSObject and UIViewController (using loadNibNamed and initWithNibName respectively). The identifier is set in the nib and is the same value passed to dequeueReusableCellWithIdentifier. I have not run this on the phone but in the simulator it's not reusing the cell.

Sep 3, 2008 9:18 AM in response to H Asseily

I hope I don't break the NDA by this, but I think a word of warning is due here: Please don't use the array returned by -loadNibNamed:owner:options: for anything but retaining it. Objects in the nib should only be accessed through the owner's outlets. You never know in which order the Nib's root objects are in the array.

Things like this are BAD practice and a very good example why Apple should lift the NDA so we can legally talk about this.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

[iPhone SDK] - reuseIdentifier with loadNibNamed

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.