使用动态高度单元格向表视图添加新行

时间:2017-08-09 05:19:33

标签: ios swift uitableview uitableviewautomaticdimension

我有一个表格视图,其中单元格具有动态高度。我想在按钮点击上添加一个新行。我正在增加节值中的行数并重新加载表视图。但这会导致崩溃。我在评论以下行后尝试了这一点

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 200
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {



    return UITableViewAutomaticDimension

}

当这两个委托方法被评论时,这工作正常。但我想添加一个新行。动态高度单元格应该是可能的。我怎样才能实现这个目标?

1 个答案:

答案 0 :(得分:1)

你可以这样做

 numberOfItems += 1
    let indexPath = IndexPath(row: self.numberOfItems - 1, section: 0)
                self.tbl.beginUpdates()
                self.tbl.insertRows(at: [indexPath], with: .automatic)
                self.tbl.endUpdates()
相关问题