首页 > 解决方案 > 如何更新tableView单元格高度

问题描述

按下按钮后,我想更新我的单元格高度,所以 atm 我使用了这个闭包并在我的按钮方法中调用他,但不工作):

我的viewDidLoad():

 tableView.estimatedRowHeight = 238.0
 tableView.rowHeight = 238.0

我的关闭尝试在按下按钮后更改单元格高度

cellClosureHeight = { [weak self] in
            self?.tableView.beginUpdates()
            self?.tableView.estimatedRowHeight = 118.0
            self?.tableView.rowHeight = 118.0
            self?.tableView.endUpdates()
        }

我的细胞初始化

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            switch productCells[indexPath.row] {
            case .popCorn(let viewModel):
                let cell = tableView.dequeueReusableCell(
                    withIdentifier: TableViewCells.popCornOrder.identifier,
                    for: indexPath
                    ) as! ProductTableViewCell

                cell.set(viewModel: viewModel)

                return cell

            case .barWater(let viewModel):
                let cell = tableView.dequeueReusableCell(
                    withIdentifier: TableViewCells.barWaterOrder.identifier,
                    for: indexPath
                    ) as! ProductTableViewCell

                cell.set(viewModel: viewModel)

                return cell
            }
        }

标签: iosswift

解决方案


没有动画

tableView.reloadData()

动画

tableView.beginUpdates()
let indexPaths = [IndexPath.init(row: 0, section: 0)] //the cells you want to update
tableView.reloadRows(at: indexPaths, with: .automatic)
tableView.endUpdates()

推荐阅读