【问题标题】:How to do increase UItableview height inside UItableviewcell?如何增加 UItableviewcell 内的 UItableview 高度?
【发布时间】:2019-04-18 13:55:44
【问题描述】:

我只是在UItableviewcell 中创建UITableview(第二个UITableview)。我需要根据单元格的第二个 UITableview 数量增加 UItableviewcell 高度(第二个 UITableview's 单元格的高度不同)。

第一个表视图:

extension ViewController:UITableViewDelegate, UITableViewDataSource{
   // no of row #1
   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "OrderTimelineCell", for: indexPath) as! OrderTimelineCell
      return cell;
   }
}

第一个表格视图单元格:

override func awakeFromNib() {
   tableView.layoutIfNeeded()
   tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height).isActive = true //always return 44*#cells
}
extension OrderTimelineCell:UITableViewDelegate, UITableViewDataSource{
   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "TimelineTableViewCell", for: indexPath) as! TimelineTableViewCell
      cell.label.text = array[indexpath.row] // text height different
      return cell
   }
}

我希望输出显示第二个UITableview 在第一个UITableview 单元格内的所有单元格。

但实际输出是单元格高度 == #no of cell * 44

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    你需要在这个方法中指定高度:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == indexOfExtendedCell {
             return 100 // or another height
        }
        return 44
    }
    

    【讨论】:

    • 不想静态指定(返回 100)。因为,动态文本进入 Cell
    • 您必须通过此方法指定它。每次更改单元格的子视图都需要在此处计算高度和更新高度。这并不可怕。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多