【发布时间】:2017-08-13 10:38:59
【问题描述】:
我有一个带有 UITableViewCell 的 UITableView,我想通过单击它来选择它们并通过再次单击它们来取消选择它们。 首先,我已经尝试过
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
// code
}
但我注意到,当您第二次单击单元格时,不会执行此功能;当您单击另一个单元格时,它会执行。 但我希望你可以点击几个单元格,当你点击它们时它们会被选中。
所以我做了这段代码(简化):
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Selected")
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LektionTableViewCell
if cell.accessoryType == .none {
print("Type = none")
cell.accessoryType = .checkmark
Lektion.insert(Dateien[indexPath.row], at: Lektion.count)
} else if cell.accessoryType == .checkmark {
print("Type = check")
cell.accessoryType = .none
Lektion.remove(at: indexPath.row)
}
}
但它不能正常工作:
- 当我单击一个单元格时,文本会自动变为“标签”(它在视图构建器中的文本)
- 当我单击带有复选标记的单元格时,复选标记不会消失,Xcode 会显示“Type = none”;这是错误的。
谁能帮帮我?
【问题讨论】:
-
代码无法工作,永远不会在
cellForRow之外调用dequeueReusableCell。将Bool属性selected添加到您的模型中,将其设置为didSelectRow并重新加载该行。在cellForRow中根据selected设置附件类型。 -
@vadian 我已经尝试过了,但我不知道如何编码。你能在回答中告诉我吗?
-
Dateien的类型是什么?顺便说一句,变量名应该以小写字母开头。请在问题中添加cellForRow。
标签: ios swift xcode uitableview cell