TableView单元格内的TableView,自动调整单元格高度

时间:2021-02-09 09:50:20

标签: ios swift uitableview

需要在每个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
        }
    }

预期输出:

enter image description here

实际输出:

enter image description here

约束:

enter image description here

3 个答案:

答案 0 :(得分:0)

解决方案:

  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。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

那么我们如何实现这一目标?一种方法是在父单元格内使用垂直轴 UIStackView(而不是 UITableView),并在不需要高度约束的情况下,对单元格的内容视图具有前导、尾随、顶部和底部约束。而不是 UITableViewCell 创建一个带有必要 UI 组件的自定义视图,并将作为 addArrangedSubview 添加到 UIStackView。随着每个视图被添加到堆栈视图中,堆栈视图的高度动态增加,进而扩展了外层 UITableViewCell 的高度,从而实现了外表自动尺寸的动态高度。