0
votes

I know similar issues have been posted before but I can't find a solution for me within them so please bear with me...

I have a tableview with a custom table cell in xcode. The cell is currently nothing more than a label:

SiteFileCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.fileNameLabel.text = [self.listDir.filesInfo objectAtIndex:indexPath.row];

This works great. I am essentially doing a directory drill down structure. When I select a cell it goes to the next level by pushing a new tableview to the navigation controller stack and reloading all the table cells.

At this point I see an often reported error:

unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

So following research I see the suggestion that I should be registering the type:

[self.tableView registerClass:[SiteFileCell class] forCellReuseIdentifier:@"FileCell"];

If I do that, the thing works well but none of the cells display any label at all!

I've seen some reference to maybe requiring custom code in the initWithStyle method of the cell's class, but I can't quite work out what would be required there, so can someone give me a bit more of a pointer please?

2
Can you post the whole of the cellForRowAtIndexPath method please. - Petar
you should instantiate your cell, just like if ( cell == nil ) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];} - deathhorse
Also, when you are going to the level, are you using the same tableView ? - Petar
If you create your custom cell in Interface Builder, you can set the identifier there and it will just work. - Marcus Adams
The answer to this depends on where you created you custom cell, so where did you create it, xib, storyboard, or code? - rdelmar

2 Answers

4
votes

Instead of registering a class register a nib. That nib is the nib where you have designed your table view cell subclass. It contains just one top-level object, the cell, and that cell has been designated a SiteFileCell. Presto, it will all just work.

See the complete explanation (with downloadable code) in my book:

http://www.apeth.com/iOSBook/ch21.html#_custom_cells

See esp. the subsection "Designing a cell in a nib".

0
votes

If you are using a XIB for cell you can set the identifier in the XIB file only.