Reputation: 11693
I couldn't find this exact question, so I am posting it.
I was looking for a way to deselect all the rows or cells that are currently selected in a UITableView
Upvotes: 5
Views: 4324
Reputation: 1
not sure if this works..
extension UITableView {
func deselectAllRows() {
guard allowsSelection else { return }
let multipleSelect = allowsMultipleSelection
allowsSelection = false
if multipleSelect {
allowsMultipleSelection = true
} else {
allowsSelection = true
}
}
}
Upvotes: -1
Reputation: 534903
Simple one-liner: to deselect all rows, select nil
! Like this
tableView.selectRow(at:nil...
(and fill out the rest of the call however you like, depending whether you want animation and scrolling).
Upvotes: 8
Reputation: 987
you can also do:
tableView.deselectRow(at: indexPath, animated: true)
inside of func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
Upvotes: 5
Reputation: 11693
I found that the simpler solution is to extend UITableView
extension UITableView {
func deselectAllRows(animated: Bool) {
guard let selectedRows = indexPathsForSelectedRows else { return }
for indexPath in selectedRows { deselectRow(at: indexPath, animated: animated) }
}
}
Upvotes: 8