我正在尝试在我正在开发的应用程序中使用 Storyboard。在应用程序中有 列表 和 用户 每个都包含另一个的集合(列表的成员,用户拥有的列表)。因此,相应地,我有 ListCell
和 UserCell
类。目标是让它们在整个应用程序中(即,在我的任何 tableview Controller 中)可重复使用。
这就是我遇到问题的地方。
如何在 Storyboard中创建可以在任何 View Controller 中重复使用的自定义 tableview 单元格?
以下是我迄今为止尝试过的具体事情。
UITableViewCell
子类,设置重用 ID,添加标签并将它们连接到类的 socket 。在 Controller #2 中,添加了一个空的原型(prototype)单元格,将其设置为与以前相同的类并重用 id。当它运行时,当单元格显示在 Controller #2 中时,标签永远不会出现。在 Controller #1 中工作正常。 viewDidLoad
中方法,将这些 NIB 文件注册为重用 ID。显示时,两个 Controller 中的单元格都像原型(prototype)一样是空的。 最佳答案
据我了解,您想要:
-[UITableView registerNib:forCellReuseIdentifier:]
。 . -[UITableView dequeueReusableCellWithIdentifier:]
[[CellClass alloc] initWithCoder:]
. -initWithCoder:
方法遍历并添加 subview 并设置在 Nib 中定义的属性。 ( IBOutlet
s 可能也被连接到这里,虽然我没有测试过;它可能发生在 -awakeFromNib
中)UITableViewCell
样式,这正是正在发生的事情。例如,“默认”样式和“字幕”样式都由相同的 UITableViewCell
表示。类。UITableView
的上下文中使用实例。In Controller #1, added a prototype cell, set the class to my UITableViewCell subclass, set the reuse id, added the labels and wired them to the class's outlets. In Controller #2, added an empty prototype cell, set it to the same class and reuse id as before. When it runs, the labels never appear when the cells are shown in Controller #2. Works fine in Controller #1.
Designed each cell type in a different NIB and wired up to the appropriate cell class. In storyboard, added an empty prototype cell and set its class and reuse id to refer to my cell class. In controllers' viewDidLoad methods, registered those NIB files for the reuse id. When shown, cells in both controllers were empty like the prototype.
-[UITableView registerNib:forCellReuseIdentifier:]
,通过 UINib
包含单元格,你会得到同一个单元格。 (这不是因为原型(prototype)“覆盖”了 Nib ;您只是没有在 tableview 中注册 Nib ,所以它仍在查看嵌入在 Storyboard中的 Nib 。)不幸的是,这种方法有一个缺陷——无法将 Storyboard segue 连接到独立 Nib 中的单元格。Kept prototypes in both controllers empty and set class and reuse id to my cell class. Constructed the cells' UI entirely in code. Cells work perfectly in all controllers.
关于objective-c - 在 Storyboard中,如何制作用于多个 Controller 的自定义单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9245969/