根据可用的文本或图像内容选择不同的 UITableViewCells

发布于 2024-11-30 17:56:22 字数 743 浏览 0 评论 0原文

我想根据可用内容显示不同的 UITableViewCell。我怎样才能做到这一点?

示例:

假设我有一个自定义 UITableViewCell,其 contentView 有一个 UILabel、UITextFieldUIImageView 作为子视图。 UILabel 位于单元格顶部,UIImageView 上方,UITextField 旁边。根据这些视图的可用内容,我想仅显示可用部分并放大其他视图。 eG:如果缺少 UILabel 上下文,我不想在单元格顶部有空白区域。如果我没有图像,文本字段应该从单元格的最左侧开始。

应该有许多报纸/电视应用程序必须解决完全相同的问题...

方法:

(i) 使用相同的自定义单元格,我以某种方式重新排列子视图

(ii) 根据可用内容调用不同的自定义UITableViewCell

对于这两种方法,我在寻找解决此问题的路径时遇到问题。我的 UITableView 内容大小是动态的,可以有几行或多行。

我是否遗漏了任何其他方法,或者如果没有,哪种方法是最佳实践?

I want to display different UITableViewCells depending on what content is available . How can I accomplish that ?

Example:

Lets say I have a custom UITableViewCell whose contentView has a UILabel, UITextField and a UIImageView as subviews. The UILabel is located on the top of the cell, above the UIImageView which is next to the UITextField. Depending on which contents for these views is available, I would like to only display the available parts and enlarge the other views. e.G: If there is missing the UILabel context I don't want to have a blank space at the top of the cell. If I don't have an image, the text field should start at the very left side of the cell.

There should be many newspaper/tv applications out there which had to solved exact the same problem...

Approaches:

(i) Using the same custom cell where I somehow rearrange the subviews

(ii) Calling different custom UITableViewCell's depending on what content is available

For both approaches I am having issues finding a path to solve this problem. My UITableView content size is dynamic and can have a few rows or many.

Is there any other approach I am missing out or if not which approach is best practice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〃安静 2024-12-07 17:56:22

你的(ii)想法就是我的想法。正如您所说,您设计了两个自定义 UITableViewCell (.m、.h 和 .xib),您不会忘记在界面生成器中定义 CellIdentifier 以及当您的委托 < code>tableView:cellForRowAtIndexPath: 被调用,您选择 dequeueReusableCellWithIdentifier: 适当的 UITableViewCell 模板。

Your (ii) idea is the way to go to my mind. As you say, you design two customs UITableViewCell (.m, .h and .xib) you don't forget to define there CellIdentifier in interface Builder and when your delegate tableView:cellForRowAtIndexPath: is called you choose to dequeueReusableCellWithIdentifier: the appropriate UITableViewCell template.

无人接听 2024-12-07 17:56:22

两种方法都有效,尽管我可能更喜欢(i)

对于(i)来说,重用很容易。只需确保在重用时清除旧数据,让 UITableViewCell 的 layoutSubviews 方法适当地放置子视图以匹配可用的任何数据,并确保在以下情况下调用 setNeedsLayout你改变数据。这里的优点是,代码的其余部分不需要关心哪些数据可用或不可用,并且您的表甚至可能不必关心单元格是否突然添加了图像(除非它需要知道单元格高度改变了或者什么的)。缺点是cell的实现比较复杂,并且布局无法在IB中完全定义。

对于(ii),每个不同的自定义 UITableViewCell 类型必须有自己的 reuseIdentifier,然后使用 dequeueReusableCellWithIdentifier: 以及适合可用数据的标识符。这里的优点是单个细胞类型更简单(并且可能完全在 IB 中完成),而缺点是对于可用数据的每种可能的组合,您必须拥有其中的多个细胞类型。另外,如果类型需要更改,您需要让 UITableView 重新加载单元格(例如使用 reloadRowsAtIndexPaths:withRowAnimation:)。

Either approach works, although I would probably prefer (i).

For (i), reuse is easy. Just make sure you clear out the old data when reusing, have your UITableViewCell's layoutSubviews method lay the subviews out appropriately to match whatever data is available, and be sure to call setNeedsLayout when you change the data. The advantage here is that the rest of your code doesn't need to care about which data is available or not, and your table may not even have to care if the cell suddenly gets an image added (unless it needs to know that the cell height changed or something). The disadvantage is that the cell's implementation is more complex, and the layout cannot be completely defined in IB.

For (ii), each different custom UITableViewCell type must have its own reuseIdentifier, and then use dequeueReusableCellWithIdentifier: with the identifier appropriate to the available data. The advantage here is that the individual cell types are simpler (and can probably be done entirely in IB), while the disadvantage is that you have to have several of them for each possible combination of available data. Also, you would need to have the UITableView reload the cell (e.g. using reloadRowsAtIndexPaths:withRowAnimation:) if the type needs to change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文