I just want to add and delete rows in a table view very elegantly. So I asked this question. But I don't think the answers answer my question.
Then I thought that I can create an array of UITableViewCell
s and use that as the data source!
So I did something like this:
class MyTableViewController: UITableViewController {
var cells = [[UITableViewCell]]()
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return cells.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cells[section].count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return cells[indexPath.section][indexPath.row]
}
func addCellToSection(section: Int, cell: UITableViewCell) {
cells[section].append(cell)
tableView.reloadData()
}
func removeCellFromSection(section: Int, index: Int) {
cells[section].removeAtIndex(index)
tableView.reloadData()
}
}
Now I can just call removeCellFromSection
and addCellToSection
to add and remove cells. How elegant!
However, I'm not sure whether calling tableView.reloadData()
all the time will slow down the app. Each time I add or remove a cell, every cell in the table view needs to be refreshed. That would be a lot of work, right?
So will calling tableView.reloadData()
all the time slow down my app significantly? If yes, what can be an alternative? If not, why not?
Additional information:
Initially, the table view contains 4 cells. The user can add cells and remove cells by pressing buttons. On average, the user would add less than 50 cells.