0
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?

1
  • 1
    Delete all items (removeAll()) in the data source array then call deleteSections() on the tableView passing the appropriate section. Commented Jan 13, 2017 at 14:30

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.