0

I made it work to to collapse the section with cells but at the end i reload all the tableview and i don't want that. What i want is to reload only the section when it expands or not (inser/delete).

Here is the method which i use to insert or delete the rows of my section:

func settingsViewTapped(view: SettingsCellView, section: Int) {
    var indexPaths = [IndexPath]()
    for row in self.settingsTrainings[section].workouts.indices {
        let indexPath = IndexPath(row: row, section: section)
        indexPaths.append(indexPath)
    }

    let isExpanded = self.settingsTrainings[section].isExpanded
    self.settingsTrainings[section].isExpanded = !isExpanded

    if isExpanded {
        tableView.deleteRows(at: indexPaths, with: .none)
        tableView.reloadData()
    } else {
        tableView.insertRows(at: indexPaths, with: .none)
        tableView.reloadData()
    }
}
Muli
  • 214
  • 5
  • 23
  • Very similar to or duplicate of this [question](https://stackoverflow.com/questions/40376728/reload-section-method-in-swift-3). – Daniel Mar 28 '20 at 19:31
  • I implemented this many times. insertion and deletion of indexPaths doesn't require to reloadData . why do you want to reloadData? – M Ohamed Zead Mar 28 '20 at 19:41

1 Answers1

0

You should wrap deleteRows and insertRows with beginUpdates and endUpdates

tableView.beginUpdates()
tableView.insertRows(at: indexPaths, with: UITableViewRowAnimation.top)
tableView.endUpdates()
Mohamed Mostafa
  • 1,057
  • 7
  • 12