UITableview reloadRows在Swift 3中无法正常工作

时间:2017-10-21 10:00:48

标签: ios swift uitableview

我创建了一个UITableView,其中多个部分包含两个具有动态单元格高度的单元格。第二个单元格上添加了一个按钮单击该按钮,通过更改约束高度来增加第二个单元格高度。连接到按钮的操作方法是:

func increaseHeightOnClickOfButton(sender: UIButton) {
    let indexPath = IndexPath(item: 1, section: sender.tag)

    let cell = myTableView.cellForRow(at: indexPath) as! customCell
    cell.viewHeightConstraint.constant = 100

    self.myTableView.reloadRows(at: [indexPath], with: .automatic)
}

问题:点击按钮后,第二部分的单元格高度增加而不是第一部分。

我也试过给出像这样的部分的修复值:

let indexPath = IndexPath(item: 1, section: 0)

但它不起作用。

仅在我尝试此代码时才有效:

func increaseHeightOnClickOfButton(sender: UIButton) {
    let indexPath = IndexPath(item: 1, section: sender.tag)

    let cell = myTableView.cellForRow(at: indexPath) as! customCell
    cell.viewHeightConstraint.constant = 100

    self.myTableView.reloadData()
}

但我不想重新加载整个表格视图。任何人都可以帮助我,我做错了什么,还是有其他方法来实现它?

修改

我做了一个更改,我删除了:

self.myTableView.reloadRows(at: [indexPath], with: .automatic)

当我开始滚动桌面视图时单击按钮后,返回到同一个单元格会增加它的高度。所以我想reloadRows存在一些问题。

3 个答案:

答案 0 :(得分:3)

您可以使用以下代码重新加载单元格的高度,而无需重新加载整个数据:

tableView.beginUpdates()
tableView.endUpdates()

答案 1 :(得分:1)

修改

修改了第一篇帖子,以包含一个DataSource来跟踪每一行的高度约束常量。

假设你的手机看起来像这样:

enter image description here

  • 您在单元格中添加了UIView(在我的示例中为橙色视图)
  • UIView
  • 上添加了标签和按钮
  • 设置视图的前导,尾随,顶部和底部约束
  • 在视图中添加了高度约束
  • 已连接IBOutletsIBAction,用于单元格类中的按钮点按
  • UITableViewAutomaticDimension
  • 设置您的表类

那么这应该是一个有效的例子。每次点击一个按钮,该单元格中的高度约束将增加20点。无需致电reload任何事情......

class TypicalCell: UITableViewCell {

    @IBOutlet weak var theLabel: UILabel!

    @IBOutlet weak var viewHeightConstraint: NSLayoutConstraint!

    var btnAction: (() -> ())?

    @IBAction func didTap(_ sender: Any) {
        btnAction?()
    }

}

class TypicalTableViewController: UITableViewController {

    // 2-D array of Row Heights
    // 4 sections, with 4, 2, 6 and 3 rows
    // all initialized to 40.0
    var rowHeights: [[CGFloat]] = [
        [40.0, 40.0, 40.0, 40.0],
        [40.0, 40.0],
        [40.0, 40.0, 40.0, 40.0, 40.0, 40.0],
        [40.0, 40.0, 40.0]
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 40.0

    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return rowHeights.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return rowHeights[section].count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Typical", for: indexPath) as! TypicalCell

        // Configure the cell...
        cell.theLabel.text = "\(indexPath)"

        // cells are reused, so set the cell's Height constraint every time
        cell.viewHeightConstraint.constant = rowHeights[indexPath.section][indexPath.row]

        // "call back" closure
        cell.btnAction = {
            // increment the value in our DataSource
            self.rowHeights[indexPath.section][indexPath.row] += 20
            // tell tableView it's being updated
            tableView.beginUpdates()
            // set the cell's Height constraint to the incremented value
            cell.viewHeightConstraint.constant = self.rowHeights[indexPath.section][indexPath.row]
            // tell tableView we're done updating - this will trigger an auto-layout pass
            tableView.endUpdates()
        }

        return cell
    }

}

答案 2 :(得分:0)

尝试重新加载章节:

self.tableView.reloadSections(IndexSet(integer: sender.tag), with: .automatic)