ios - 是否可以使用 reuseIdentifier 自定义具有不同内容的 TableViewCells?

标签 ios objective-c uitableview

我有一个最多包含 50 个单元格的 TableView,其中包含 5 个包含公共(public)信息(标题、描述、价格...)的 subview 和 5 个可选 subview (报价、报价过期...)。这些是通过添加所有内容然后隐藏不适合该单元格的可选 View 来构建的。

使用通用的 reuseIdentifier 意味着所有单元格的布局都与最后构建和缓存的单元格相同。所以最初我已经使用 reuseIdentifier:nil 并且它有效,但是每次显示它们时重建它们似乎是一个糟糕的性能打击。有些可以跨单元重复使用,因为 <5!变体,但这可能很难维护,所以如果我至少可以单独缓存它们,我会更高兴。

我已经尝试过 reuseIdentifier:[NSString stringWithFormat:@"%li", (long) indexPath.row] 但得到:

*** Terminating app due to uncaught exception 
'NSInternalInconsistencyException', reason: 'unable to dequeue a cell 
with identifier 1 - must register a nib or a class for the identifier or 
connect a prototype cell in a storyboard'

我不知道如何使用 [self.tableView registerClass:... forCellReuseIdentifier:...] 初始化 TableView 以允许这样做,但其他人已经使用了这种或类似的方法那我错过了什么?

Custom UITableViewCell with reuseIdentifier without Style?

how to use UItableViewCell reuseIdentifier

我正在处理一个继承的项目,只有几天的 obj-C 经验,并且遇到了一些恐怖的事情,所以整个方法可能值得怀疑。所有 View 都是以编程方式完成的。如果需要,我可以添加代码片段。

干杯!

最佳答案

在单元的类实现中实现prepareForReuse。将 View 重置为其默认状态。在 tableView:cellForRowAtIndexPath: 中,根据要显示的特定数据的需要配置单元格。

关于ios - 是否可以使用 reuseIdentifier 自定义具有不同内容的 TableViewCells?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33729196/

相关文章:

ios - 使用 Core Animation 的 UIView 翻转动画

ios - 核心数据与完整数据存储的子集一起工作...如何?

ios - 如何在编辑 uitextfield 时停止在 uitableview 中滚动

xcode - UITableViewAutomaticDimension 在 Xcode 6.3 中不起作用

ios - UITableViewCell - 更新 NSMutableDictionary 值

ios - swift 3 从 textField 中删除大量文本

ios 7.0 模拟器不会在 xcode 6.0.1 中下载

ios - 是否可以在一个单元格中推送 2 个表格 View ?

iphone - 将 Twitter 与 sharekit 2.0 集成

ios - 如何解析未命名数组的 NSDictionary