布局 UITableView 内容

发布于 01-14 17:57 字数 855 浏览 2 评论 0原文

标题本身可能有点误导,我相信最好用一个例子来解释这个问题。

以 Facebook 的 feed 为例。每个项目由以下部分组成:

  • 标题(用户名等),静态高度
  • 主要内容(文本、附件),动态高度
  • 页脚(喜欢的数量和几个按钮),静态高度

主要内容至少有一个以下内容:

  • 标题,可选
  • 文本预览,可选
  • 附件(可变高度),通常是图像,可选

那么,一些可能的部分组合:

  • 标题
  • 标题、文本
  • 标题、文本、附件
  • 文本
  • 文本、附件

我来了有两种方法:

1. 部分对于每个帖子,各部分的单元格

注意:UIKit 提供的页眉和页脚不起作用,因为我需要每个项目之间的特定间距。因此,我会使用它们来获得所需的间距。

另外,我正在使用 RxSwift。这种方法打破了数据绑定到表视图的简单性,尽管总有更复杂的替代方案。

2. 多种细胞类型

不确定。为上述每种可能的组合提供单独的细胞类型?看起来有点矫枉过正。

3. 单细胞类型

最简单的方法。但是,考虑到所有可选部件,切换和调整有相当多的限制(取决于附件高度)。 UIStackView 显然不是一个选项,因为附件必须占据整个屏幕宽度,而其他部分在每个轴上使用自定义填充。

我在这里寻找外部观点,主要来自那些过去碰巧解决过类似问题的人。代码示例也很不错(特别是如果它是可扩展的)。

目标:iOS 13+。

AutoLayout 在这些方面都表现良好吗?
您会使用什么方法?

The title itself might a bit misleading and I believe the problem is best explained with an example.

Take Facebook's feed, for instance. Every item consists of the following:

  • Header (user's name, etc), static height
  • Main content (text, attachment), dynamic height
  • Footer (number of likes and a couple of buttons), static height

At a minimum, the main content has one of the following:

  • Title, optional
  • Textual preview, optional
  • Attachment (of variable height), usually an image, optional

So, a few possible combinations of parts:

  • Title
  • Title, text
  • Title, text, attachment
  • Text
  • Text, attachment
  • etc

I've come up with two approaches:

1. Sections for each post, cells for parts

Note: the UIKit-provided header and footer wouldn't do, as I need specific spacing between each item. Therefore, I'd use these to get the spacing needed.

Also, I'm using RxSwift. This approach breaks the simplicity of data binding to the table view, although there's always a more complex alternative.

2. Multiple cell types

Not sure. Separate cell type for each possible combination of the above? Seems like an overkill.

3. Single cell type

The easiest approach. But, considering all the optional parts, there's quite a few constraints to switch and tweak (depending on the attachment height). UIStackView is not an option evidently, since the attachment must take the whole screen width, whilst the other parts use custom padding on each axis.

I'm looking for an outside perspective here, mainly from those who happen to have solved a similar problem in the past. An example of code would also be lovely (especially if it's scalable).

Target: iOS 13+.

Will AutoLayout perform alright in each of these?
What approach would you use?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文