Swift 3:为什么我不能在didDeselectRowAt中更改cell.accessoryType和cell.textLabel

Sam*_*her 2 uitableview didselectrowatindexpath ios swift swift3

我无法理解为什么我无法切换选定的表格行.我在didSelectRowAt中成功设置了accessoryType.但我似乎无法在didDeselectRowAt中将accessoryType设置为none.

在此输入图像描述

我的数据:

serviceItems = ["Heater", "Air Conditioner", "Boiler", "Heat Pump"]
Run Code Online (Sandbox Code Playgroud)

这是我的tableview覆盖:

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.accessoryType = .checkmark
    let serviceItem = serviceItems[indexPath.row] as! String
    cell.textLabel?.text = serviceItem
}

// does not remove checkmark
// does not change textLabel to "test" 
// DOES print "DESELECTING" (so I know its getting called)
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.accessoryType = .none
    cell.textLabel?.text = "test"
    print("DESELECTING")
}
Run Code Online (Sandbox Code Playgroud)

Sam*_*her 6

应该用tableView.cellForRow而不是tableView.dequeueReusableCell

更改:

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
Run Code Online (Sandbox Code Playgroud)

至:

let cell = tableView.cellForRow(at: indexPath)
Run Code Online (Sandbox Code Playgroud)