Custom Table Cell Reuse / Dequeue

a 夏天 提交于 2019-12-23 04:41:13

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/15813498/custom-table-cell-reuse-dequeue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!