Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top