user30646 user30646 - 1 month ago 7
iOS Question

How do I retrieve a collection of selected cells

I have a UITableViewController with several cells. Users can enable select feature by tapping on Select button. Once user taps on it, it toggles to Cancel button. What I would like to do is cancels all current selections when user taps Cancel. It should work just like Photo app. I know how to deselect a selected cell but I don't how to retrieve a collection of selected cells, so that I can iterate through it and deselect them. The following is my attempt, but as you can see, it only deselect one cell.

func deselectAll()
{
if let index = self.tableView.indexPathForSelectedRow{
self.tableView.deselectRowAtIndexPath(index, animated: true)
}
}

Answer

You can use the table view's indexPathsForSelectedRows property to get an array of selected NSIndexPaths. Loop over this array and deselect each one.

Comments