首页 > 解决方案 > 何时更新 tableView?

问题描述

我正在构建一个待办事项列表应用程序,我在其中使用我的部分标题来显示有关该过程的信息。通过字符串操作,我在其中显示了一些关于 To-do 的值。但是要始终保持最新状态,我只知道reloadData()命令。但是尝试自动删除行看起来真的很糟糕。

所以我的问题是如何以干净的方式更新标题文本,而不是通过reloadData().

这是您将待办事项标记为已完成的地方:

alert.addAction(UIAlertAction(title: "Done", style: .default, handler: { (action) in

        if indexPath.section == 0 {
            self.goals.remove(at: indexPath.row)
        } else {
            self.goalsB.remove(at: indexPath.row)
        }
        self.counter+=1
        self.saveDefaults()
        tableView.deleteRows(at: [indexPath], with: .automatic)
    }))

这就是我的 Header 设置的地方:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let label = UILabel()
    label.textColor = UIColor.white
    label.backgroundColor = section == 0 ? UIColor(red: 41 / 255.0, green: 76 / 255.0, blue: 103 / 255.0, alpha: 1.0) : UIColor(red: 235 / 255.0, green: 108 / 255.0, blue: 33 / 255.0, alpha: 0.5)

    if section == 0 {

        label.text = "     \(counter) goals completed / \(goals.count) outstanding"

    }

    return label
}

标签: iosswiftuitableview

解决方案


如果您事先知道必须更新哪个特定部分的标题,则可以使用 TableView.reloadSection() 方法。


推荐阅读