【问题标题】:TableView inside TableView cell, adjust cell height automaticallyTableView单元格内的TableView,自动调整单元格高度
【发布时间】:2021-02-09 09:50:20
【问题描述】:

每个tableviewcell中需要显示一个内容列表,所以这里我在Tableview cell里面使用了tableview。但问题是主 tableview 单元格高度不会根据内容而变化。我不想修复 UITableViewCell 的高度,我希望它根据内容动态调整。

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! SubCell
        
        if indexPath.row%2==0{
            cell.backgroundColor = .orange
        }else{
            cell.backgroundColor = .green
        }
        return cell
    }

    class SubCell: UITableViewCell, UITableViewDelegate,UITableViewDataSource {
        
        
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableView.automaticDimension
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 4
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell2") as! SubCell2
            
            cell.titleLabel.text = "asdf asdf asdf asdf asdf adfs asdf asdf asdf asdf asdf asdf asd fasd fasd fasd fasd fasd fasd f asdf asdf asdf asdf asd fasd fasd fasd fasd fasd fasdf asdf asd fasd 1234"
            return cell
            
        }
        
        @IBOutlet weak var tablView2: UITableView!
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
                    
        }
        
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
            
        }
        
    }

    class SubCell2: UITableViewCell {
        
            
        @IBOutlet weak var titleLabel: UILabel!
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
            
            
        }
        
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
            
            // Configure the view for the selected state
        }
    }

预期输出:

实际输出:

约束:

【问题讨论】:

  • 如何在单元格中设置约束?这就是“魔法”发生的地方。
  • 我添加了另一个截图,请检查。 @Larme
  • 您是否检查过:stackoverflow.com/questions/18746929/…?另外,你在 UITableViewCell 中有一个 UITableView 吗?
  • 我做了,但没用@Larme ...,
  • @Codecracker - 在 tableView 单元格中“嵌套”tableViews 本质上是有问题的,并且很少给你想要的结果。您是否有不想使用多个 tableView 部分的原因?

标签: ios swift uitableview


【解决方案1】:

解决方案:

  1. 将标签约束 Bottom space to container 设置为它的超级视图(内容视图)。
  2. 现在select "yourTableView" 尺寸检查员使其成为"Row height as Automatic"
  3. 现在select "yourTableViewCell" 在尺寸检查员中使其成为"Row height as Automatic"

由于基于标签高度,the content view will be Resized,并且tableViewcell和tableview的高度分配是自动调整的。

我已发布上述答案仅适用于 StoryBoard 和 Xib。

【讨论】:

  • 不走运@Dilip M
【解决方案2】:

您的标题标签没有任何高度设置。如果您设置一个,您的单元格将动态运行。

您可以向标题标签添加另一个约束,并将高度设置为“大于或等于”您想要以任何数字开头(例如 30)。只要您将标题标签的行数设置为 0,那么标题标签的高度就应该动态增长,并且仍然保持与其他约束相同的填充。

【讨论】:

    【解决方案3】:

    在动态内容的情况下,我们需要在 UITableViewCell 中放置 UITableView,Automatic Dimension 似乎不起作用,因为外部 UITableView 需要为其拥有的每个单元格设置高度。当需要这个高度时,子 UITableView 还没有加载自己的单元格,因此编译器无法在运行时获取高度

    那么我们如何实现呢?一种方法是在父单元格内使用垂直轴 UIStackView(而不是 UITableView),相对于单元格的内容视图具有前导、尾随、顶部和底部约束,而无需任何高度约束。代替 UITableViewCell 创建一个包含必要 UI 组件的自定义视图,并将其作为 addArrangedSubview 添加到 UIStackView。随着每个视图被添加到堆栈视图中,堆栈视图的高度会动态增加,这反过来又会扩展外部 UITableViewCell 的高度,从而实现动态高度以及外部表格的自动尺寸。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 2017-02-28
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多