具有动态高度的 UITableViewCell 中的 UITableView [英] UITableView within a UITableViewCell with dynamic height

查看:43
本文介绍了具有动态高度的 UITableViewCell 中的 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图构建一些相当复杂的东西 - 可能比下图显示的更多.总之..

So I'm trying to build something rather intricate - potentially more than what the picture below shows. Anyway..

我正在尝试在父 UITableView 的单元格内放置一个 UITableView.然而,这通常不是很复杂,我需要包含嵌套 tableView 的单元格随着它持有的 tableView 根据它的内容增长和缩小而动态调整大小.不应允许嵌套的 tableView 在单元格内独立滚动.

I'm trying to have a UITableView inside of a cell of a parent UITableView. This is not very complex usually however, I need the cell containing the nested tableView to resize dynamically as the tableView it holds grows and shrinks according to it's contents. The nested tableView shouldn't be allowed to scroll independently within the cell.

我被困了一天又一天.我已经尝试了我能想到的所有方法,我在网上找到的所有内容以及几乎所有关于 SO 的建议.

I've been stuck now for a day and a bit. i've tried everything I can think of, everything I've found online and virtually every suggestion here on SO.

我尝试将 tableView 高度约束(并动态更新)链接到 tableView 的 contentSize.我认为这应该有效,但它没有(打破其他限制).

I've tried linking the tableView height constraint (and updating it on the fly) to the tableView's contentSize. This should work I reckon but it doesn't (breaks other constraints).

任何建议或其他解决方法将不胜感激:)

Any suggestions or other ways to approach this would be appreciated :)

红色显然是嵌套的tableView,蓝色是外层.如您所见,包含嵌套 tableView 的单元格"已根据其内容调整大小.

The red is obviously the nested tableView, the blue is the outer one. As you can see, the "cell" containing the nested tableView has sized according to its contents.

推荐答案

您可以子类化 UITableView 并根据其内容将其转换为自动调整大小"的表格视图:

You can subclass UITableView and turn it into an "auto-sizing" table view, based on its contents:

final class ContentSizedTableView: UITableView {
    override var contentSize:CGSize {
        didSet {
            invalidateIntrinsicContentSize()
        }
    }
    override var intrinsicContentSize: CGSize {
        layoutIfNeeded()
        return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
    }
}

现在,表格视图的行为与多行 UILabel 非常相似——只需以相同的方式设置约束.

Now, the table view will behave very similar to a multi-line UILabel --- just set up your constraints in the same way.

两个注意事项:

1) 禁用 ContentSizedTableView 上的滚动 - 不是 必需,但对于您的情况可能会是更好的 UX.

1) Disable scrolling on the ContentSizedTableView - not necessary but will probably be a better UX for your case.

2) 当您在原型单元格中进行布局时,它需要一个高度约束来满足 IB/Storyboard.所以,添加

2) When you lay this out in your prototype cell, it will need a Height constraint to satisfy IB / Storyboard. So, add either

  • a >= 高度约束(因此即使没有行,您也有最小高度),或
  • 给它一个占位符"高度限制,或者
  • 给高度限制一个低优先级
  • a >= height constraint (so you have a minimum height even if you have no rows), or
  • give it a "Placeholder" height constraint, or
  • give the height constraint a low priority

这是在滚动视图中使用 ContentSizedTableView 的示例.同样的想法,应该清楚如何使用它:

Here is an example using a ContentSizedTableView in a scroll view. Same idea, and should make it clear how to use it:

https://stackoverflow.com/a/56840758/6257435

这篇关于具有动态高度的 UITableViewCell 中的 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆