【发布时间】:2017-02-28 21:34:16
【问题描述】:
我有一个表格视图,用户可以在其中选择多行(这些行由行中的复选框区分)。但是,由于某种原因,我无法实现取消选择任何选定行的功能。谁能告诉我我错过了什么?
SomeViewController.m
@objc class SomeViewController: UIViewController, NSFetchedResultsControllerDelegate, UITableViewDataSource, UITableViewDelegate {
var deviceArray:[Device] = []
// [perform a fetch]
// [insert fetch results into deviceArray to be displayed in the tableview]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customcell", for:
indexPath)
// Set up the cell
let device = self.deviceArray[indexPath.row]
cell.textLabel?.text = device.name
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView(tableView, cellForRowAt: indexPath).accessoryType = UITableViewCellAccessoryType.checkmark
NSLog("Selected Row")
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
self.tableView(tableView, cellForRowAt: indexPath).accessoryType = UITableViewCellAccessoryType.none
NSLog("Deselected Row")
}
}
更多信息: 查看插入的调试日志,我可以分享以下观察结果:
-
选择未选择的行时,控制台打印“选择行”
如果我单击观察 #1 中的同一行,控制台仅打印“选定行”
如果我点击任何其他行,控制台会打印“取消选定的行”,然后打印“选定的行”
如果我单击与观察 #3 相同的行,控制台将仅打印“选定行”。
所以,看起来每次我点击不同的行时,tableView: didDeselectRowAt: 都会被调用;但是,单击的行中的复选标记不会消失。
更多信息 2:
所以我是故事板的新手,没有设置“allowsMultipleSelection”属性。进入属性检查器,这就是我的设置:
现在,当按下 tableView 中的同一行时,我的控制台确认应用程序在 tableView:didSelectRowAt: 和 tableView:didDeselectRowAt: 之间交替,但是,复选标记并没有消失;一旦用户选择了一行,即使在调用 tableView:didDeselectRowAt: 时,复选标记仍保持选中状态。我还缺少什么?
【问题讨论】:
标签: ios uitableview swift3