静态单元格中的复选框

Checkbox in Static Cell

我是 Swift 的新手,我开始学习 table 中的复选框,但我的 table 工作正常 我使用静态单元格:

我的代码:

 var flagCell : Bool = false

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.separatorInset = .zero
    tableView.layoutMargins = .zero

    let nav = self.navigationController?.navigationBar
    nav?.tintColor = UIColor(red: 16.0, green: 18.0, blue: 34.0, alpha: 1.0)
    nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        if flagCell  == false{
            cell.accessoryType = .checkmark
            flagCell = true
        }else {
            cell.accessoryType = .none
            flagCell = false
        }
    }
}

标记时间。

How can it solve?

我也想做UISwith si on,勾号出现在所有Cell

How can I implement it ?

将此代码放入 cellForRowAtIndexPath

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellID")
    if flagCell  == false {
      cell.accessoryType = .checkmark
    } else {
      cell.accessoryType = .none
    }
  }

并在录音后重新加载表格

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   flagCell = !flagCell
   tableView.reloadData()
}

或者您可以使用其他方法更新单元格:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       flagCell = !flagCell
       tableView.reloadRows(at: [indexPath], with: .automatic)
    }