ben fo ben fo - 1 year ago 165
Swift Question

How to deselect selected row

How can i deselect a row when another row is selected here i have this in my cellForRowAtIndexPath

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

And this in 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

Answer Source

As per my understanding, you want to remove the check mark of a previously selected cell when another cell is selected. Since in your cellForRowAtIndexPath method, you already correctly set up the accessoryType, any new cell will have a correct check mark status. The only work left is to remove the check mark on existing cells, if any. You can get the existing cells from the visibleCells of the table view. One possible solution is:

tableView.visibleCells.filter { $0 != cell }.forEach { $0.accessoryType = .None }

If you prefer for-loop:

for visibleCell in tableView.visibleCells where visibleCell != cell {
    visibleCell.accessoryType = .None
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download