In a storyboard, how do I make a custom cell for use with multiple controllers?

10/14 03:13
阅读数 0

问题:

I'm trying to use storyboards in an app I'm working on.我正在尝试在我正在开发的应用程序中使用故事板。 In the app there are Lists and Users and each contains a collection of the other (members of a list, lists owned by a user).在应用程序中有列表用户,每个都包含另一个集合(列表的成员,用户拥有的列表)。 So, accordingly, I have ListCell and UserCell classes.因此,相应地,我有ListCellUserCell类。 The goal is to have those be re-usable throughout the app (ie, in any of my tableview controllers).目标是让它们在整个应用程序中(即,在我的任何 tableview 控制器中)可重复使用。

That's where I'm running into a problem.这就是我遇到问题的地方。

How do I create a custom tableview cell in the storyboard that can be re-used in any view controller?如何在故事板中创建可以在任何视图控制器中重复使用的自定义 tableview 单元格?

Here are the specific things I've tried so far.以下是我迄今为止尝试过的具体事情。

  • 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.在控制器 #1 中,添加了一个原型单元,将类设置为我的UITableViewCell子类,设置重用 ID,添加标签并将它们连接到类的插座。 In Controller #2, added an empty prototype cell, set it to the same class and reuse id as before.在 Controller #2 中,添加了一个空的原型单元格,将其设置为与以前相同的类并重用 id。 When it runs, the labels never appear when the cells are shown in Controller #2.当它运行时,当单元格显示在控制器 #2 中时,标签永远不会出现。 Works fine in Controller #1.在控制器 #1 中工作正常。

  • Designed each cell type in a different NIB and wired up to the appropriate cell class.在不同的 NIB 中设计每种细胞类型并连接到适当的细胞类别。 In storyboard, added an empty prototype cell and set its class and reuse id to refer to my cell class.在故事板中,添加了一个空的原型单元格并设置其类并重用 id 来引用我的单元格类。 In controllers' viewDidLoad methods, registered those NIB files for the reuse id.在控制器的viewDidLoad方法中,为重用 ID 注册这些 NIB 文件。 When shown, cells in both controllers were empty like the prototype.显示时,两个控制器中的单元格都像原型一样是空的。

  • Kept prototypes in both controllers empty and set class and reuse id to my cell class.将两个控制器中的原型保持为空并设置类并将 id 重用于我的单元类。 Constructed the cells' UI entirely in code.完全在代码中构建单元格的 UI。 Cells work perfectly in all controllers.单元格在所有控制器中都能完美运行。

In the second case I suspect that the prototype is always overriding the NIB and if I killed the prototype cells, registering my NIB for the reuse id would work.在第二种情况下,我怀疑原型总是覆盖 NIB,如果我杀死了原型单元,则将我的 NIB 注册为重用 ID 会起作用。 But then I wouldn't be able to setup segues from the cells to other frames, which is really the whole point of using storyboards.但是我将无法设置从单元格到其他帧的转场,这才是使用故事板的真正意义所在。

At the end of the day, I want two things: wire up tableview based flows in the storyboard and define cell layouts visually rather than in code.归根结底,我想要两件事:在故事板中连接基于 tableview 的流,并以视觉方式而不是在代码中定义单元格布局。 I can't see how to get both of those so far.到目前为止,我不知道如何获得这两个。


解决方案:

参考一: https://en.stackoom.com/question/cnIX
参考二: https://stackoom.com/question/cnIX
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部