【发布时间】:2021-08-30 02:01:06
【问题描述】:
当我点击一行时,我期望发生的事情:该行中的文本会立即打印,灰色背景闪烁,复选标记会立即打开/关闭。
实际发生的情况:在我点击不同的行之前不会打印文本,灰色背景不会闪烁,只有在我点击不同的行时才会更新复选标记。
let itemArray = ["Find Mike", "Buy Eggos", "Destroy Demagorgon"]
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
print(itemArray[indexPath.row])
if tableView.cellForRow(at: indexPath)?.accessoryType == .checkmark {
tableView.cellForRow(at: indexPath)?.accessoryType = .none
} else {
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
tableView.deselectRow(at: indexPath, animated: true)
}
我不知道为什么这没有按我预期的方式工作。我使用 print 语句检查函数何时被调用,但我无法忘记它只是没有在我期望的时候调用的事实。
回答
这是一个语法错误,我使用的是 didDeselectRowAt 而不是 didSelectRowAt。
【问题讨论】:
-
您实现了 didDeselectRowAt。尽管如此,永远不要修改 view(单元格),修改 model 并告诉 controller 更新 UI。
-
哇,就是这样,只是语法错误。谢谢,这让我发疯了。
-
无论如何你的方法都是错误的。当用户滚动时,单元格被重复使用并丢失信息。请看stackoverflow.com/questions/67523075/…
-
哈哈,去过那里,做到了。这些方法一看就很像。
标签: ios swift xcode uitableview