自定义表格单元格重用/出队 [英] Custom Table Cell Reuse / Dequeue

查看:30
本文介绍了自定义表格单元格重用/出队的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前已经发布过类似的问题,但我无法在其中找到适合我的解决方案,所以请耐心等待...

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

我在 xcode 中有一个带有自定义表格单元格的表格视图.单元格目前只不过是一个标签:

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];

这很好用.我本质上是在做一个目录钻取结构.当我选择一个单元格时,它会通过将新的 tableview 推送到导航控制器堆栈并重新加载所有表格单元格来进入下一个级别.

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:

无法使带有标识符 Cell 的单元出列 - 必须为标识符注册一个笔尖或类,或者连接故事板中的原型单元

所以经过研究,我看到了我应该注册类型的建议:

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!

我已经看到一些可能需要在单元格类的 initWithStyle 方法中使用自定义代码的参考,但我无法弄清楚那里需要什么,所以有人可以给我更多的指针吗?

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?

推荐答案

注册一个 nib 而不是注册 class.该笔尖是您设计表格视图单元子类的笔尖.它只包含一个顶级对象,即单元格,并且该单元格已被指定为 SiteFileCell.Presto,一切都会好起来的.

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

参见 esp.在笔尖中设计单元格"小节.

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

这篇关于自定义表格单元格重用/出队的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆