Reputation: 93
let indexPaths = (0..<array.count - 1).map { i in
return IndexPath(item: i, section: section)
}
tableView.beginUpdates()
if hidden[section] {
array.remove(at: IndexPath(item: item, section: section).row)
tableView.deleteRows(at: indexPaths, with: .fade)
} else {
tableView.insertRows(at: indexPaths, with: .fade)
}
tableView.endUpdates()
I want to delete all cells of a section in UITableView when a header of that section is tapped. However, it does not seem to work correctly.
The error says that "Cannot convert value of type CountableRange<Int> to expected argument Int".
What is the correct way to delete all cells?
Upvotes: 0
Views: 1007
Reputation: 648
From seeing your code you are filling your tableView with data from an array.
If so you could detect the click on the section header and when that header is tapped. Then you call:
array.removeAll()
And when that is executed:
tableView.reloadData()
And you can detect the tap on the section header with:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { /*to acces the section indexPath.section*/ }
Then all the cells are deleted because there is no data to fill the tableView with.
Upvotes: 2