简体   繁体   English

每个表有多种自定义单元格类型 UITableView (iPhone)

[英]Multiple custom cell types per table UITableView (iPhone)

I've been searching everywhere on Apple.Developer and Stackflow and haven't come to a clear, standard and proper way of handling this.我一直在 Apple.Developer 和 Stackflow 上到处搜索,但还没有找到一种清晰、标准和正确的处理方式。 Basically what I'm looking to do is how to represent more than one customized cell type in a table.基本上我要做的是如何在表格中表示多个自定义单元格类型。 Also, there are other tables in the app that will potentially reuse some customized cells.此外,应用程序中还有其他表格可能会重用一些自定义单元格。

Most of the time, tables will have the same cell type repeated over and over.大多数情况下,表格会一遍又一遍地重复相同的单元格类型。 But in a few cases, I have a table with a couple types of complex custom cells.但在少数情况下,我有一个包含几种复杂自定义单元格的表格。

Listed below are the different ways I've learned you can do this.下面列出了我学到的不同方法可以做到这一点。 I've tried each one and they all work but I'm unsure of the "proper" way and a way that Apple won't reject.我已经尝试了每一个,它们都可以工作,但我不确定“正确”的方式以及 Apple 不会拒绝的方式。

1) programatically - ultimately, this is the best choice for performance. 1)以编程方式 - 最终,这是性能的最佳选择。 But if there's a way that I can draw complex customized cells in Interface Builder, it would be the better choice.但如果有一种方法可以在 Interface Builder 中绘制复杂的自定义单元格,那将是更好的选择。 This is what I'm trying to avoid.这是我试图避免的。

2) a customized cell class with a separate XIB - each customized cell has its own header/implementation/XIB and is a subclass of UITableViewCell. 2)带有单独 XIB 的自定义单元 class - 每个自定义单元都有自己的标题/实现/XIB,并且是 UITableViewCell 的子类。 A single cell in a XIB has its file owner set to a specific UITableViewController class. XIB 中的单个单元格将其文件所有者设置为特定的 UITableViewController class。 The View here has also been removed.此处的视图也已被删除。

3) a customized cell in the same XIB as the view controller's XIB - I read that this is bad. 3)与视图控制器的 XIB 在同一 XIB 中的自定义单元格- 我读到这很糟糕。 A controller should only be associated with a single view in the hierarchy. controller 应该只与层次结构中的单个视图相关联。 I included a customized cell in the XIB at the same level as the view and then just linked it up to the controller's IBOutlet.我在 XIB 中包含了一个与视图相同级别的自定义单元,然后将其链接到控制器的 IBOutlet。

4) a separate common XIB - this is essentially a dumping ground for customized cells . 4)一个单独的通用XIB——这本质上是一个定制电池的倾销场 The view here has also been removed.这里的视图也已被删除。 The XIB contains multiple cells and each cell is associated with a specific subclass of UITableViewCell. XIB 包含多个单元格,每个单元格都与 UITableViewCell 的特定子类相关联。 A header and implementation file contain the definition and implementation for each cell that is subclassed from UITableViewCell. header 和实现文件包含从 UITableViewCell 子类化的每个单元的定义和实现。 In the controller that is showing the table, the method cellForRowAtIndexPath, loops through the NSBundle for this common XIB and finds the cell associated with a specific subclass type of UITableViewCell.在显示表格的 controller 中,方法 cellForRowAtIndexPath 循环遍历此公共 XIB 的 NSBundle,并找到与特定子类类型的 UITableViewCell 关联的单元格。 Then it returns that.然后它返回。 This works beautifully for reuse throughout the app but something is telling me it's bad.这可以很好地在整个应用程序中重复使用,但有些东西告诉我它很糟糕。

What would be the proper way for different cell types in a single table?对于单个表格中的不同单元格类型,正确的方法是什么?

I have found that option #2, a custom cell class with a separate XIB, is the easiest to maintain.我发现选项 #2,一个带有单独 XIB 的自定义单元 class,是最容易维护的。

Creating everything in the code will run faster, but when I'm developing, it takes more time/effort to get into the flow of modifying the code.在代码中创建所有内容会运行得更快,但是当我在开发时,需要更多的时间/精力来进入修改代码的流程。 In the end, ease of maintenance is more important than a minor hit in performance.最后,易于维护比对性能的轻微影响更重要。

Simply, if it works for you then go for it.简单地说,如果它适合你,那么 go 就可以了。 All of these ideas are viable solutions.所有这些想法都是可行的解决方案。

Personally, I'd go for them in this order: 2, 1, 4, 3. Though that's just my personal preference.就个人而言,我会按以下顺序为他们选择 go:2、1、4、3。尽管这只是我个人的偏好。

PS None of these ideas are going to get your app rejected by Apple. PS 这些想法都不会让您的应用程序被 Apple 拒绝。

I don't see any drawbacks for your method 4, which is a variant of method 2 (using just one xib for all cells instead of one for each of them).我看不出您的方法 4 有任何缺点,它是方法 2 的一种变体(所有单元格仅使用一个 xib,而不是每个单元格使用一个 xib)。

I don't see any need for defining the cell programmatically, nor to bloat the xib associated to your main view controller with all the custom cells (that you could reuse elsewhere).我认为不需要以编程方式定义单元格,也不需要将与您的主视图 controller 关联的 xib 与所有自定义单元格(您可以在其他地方重用)一起膨胀。

Implementing option 2 or 4 does not entail a big difference.实施选项 2 或 4 不会带来很大的不同。 With option 2 you load the nibs one by one, with option 4 you load the unique nib, then manage the array of objects (views/cells in your case) that you get.使用选项 2,您一个一个地加载笔尖,使用选项 4,您加载唯一的笔尖,然后管理您获得的对象数组(在您的情况下为视图/单元格)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM