如何取消选择选定的行

How to deselect selected row

如何在此处选择另一行时取消选择一行我在我的 cellForRowAtIndexPath 中有这个

if ff.isVisitedd{
        cell.accessoryType = .Checkmark
    }else{
        cell.accessoryType = .None
 }

这在 didSelectRowAtIndexpath

if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        let selectedRows = tableView.indexPathsForSelectedRows! as? [NSIndexPath]
        let ff: Card!
        ff = cards[indexPath.row]
        ff.isVisitedd = true
        cell.accessoryType = .Checkmark
    }

Swift 3

您可以使用deselectRow(at:,animated:)方法。

Swift 2

deselectRowAtIndexPath(indexPath:,animated:)

据我了解,您想在选择另一个单元格时删除先前选择的单元格的复选标记。由于在您的 cellForRowAtIndexPath 方法中,您已经正确设置了 accessoryType,因此任何新单元格都将具有正确的复选标记状态。剩下的唯一工作就是删除现有单元格上的复选标记(如果有)。您可以从 table 视图的 visibleCells 中获取现有单元格。一种可能的解决方案是:

tableView.visibleCells.filter { [=10=] != cell }.forEach { [=10=].accessoryType = .None }

如果你更喜欢for循环:

for visibleCell in tableView.visibleCells where visibleCell != cell {
    visibleCell.accessoryType = .None
}