ios - 如何在tableview Cell中设置tableView的自动高度?

标签 ios swift uitableview

我有一个问题困扰了我一段时间。如何为另一个 tableview 单元格内的 tableview 设置自动高度?

所以我有 MainTableView,里面有 3 个单元格 (这个 tableview 有自动高度)3rd 单元格我在里面有另一个 tableView (我们称它为 SecondaryTableView,它对其 superview 有约束)

Image1

Image2

事情是前 2 个单元格的自动高度。

对于 SecondaryTableView 的设置,我尝试执行相同的操作,但对于单元格,我选择了 xib 文件。

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        tableView.dataSource = self
        tableView.delegate = self
        tableView.estimatedRowHeight = 900
        tableView.rowHeight = UITableView.automaticDimension
        registerCells()
    }

    private func registerCells() {
        tableView.register(UINib(nibName: "MainCategorySectionTableViewCell", bundle: nil), forCellReuseIdentifier: "MainCategorySectionTableViewCell")
        tableView.register(UINib(nibName: "AdPerHomePageTableViewCell", bundle: nil), forCellReuseIdentifier: "AdPerHomePageTableViewCell")
    }

我附上了一张它在模拟器中的外观图片。绿色是第一个单元格,橙色是第二个单元格(已注册)。我稍微滚动了前 2 个单元格以查看差异。

Output

可能是单元格上没有数据?但是我贴了一些标签,看起来一样

最佳答案

您需要使用 UITableView 的 intrinsicContentSize 变量。

Create a subclass for child tableView and override intrinsicContentSize.

Dynamic row heights of a UITableView inside a UITableViewCell

关于ios - 如何在tableview Cell中设置tableView的自动高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57988755/

相关文章:

ios - 在 api 类之外显示与 alamofire 相关的警报

ios - 通过按 UIButton 将 UILabel 的文本更改为随机数组对象

ios - 升级到 iOS 6 后出现 NSInternalInconsistencyException

ios - 以编程方式将 subview 添加到自定义 UITableViewCell

ios - 禁用 iPhone 键盘按钮

iphone - 编写一个 "No Photos In Albums"屏幕

swift - 天气 Swift 首选项

ios - 创建拉伸(stretch)的半圆

ios - 在搜索栏上方添加空间

ios - 自定义tableview滚动效果,如google底漆