CAN CAN - 13 days ago 8
iOS Question

Reload tableView section without reloading header section - Swift

I'm trying to reload tableView section instead of reloading the whole tableview because I have a textfield in the header section and when I call

self.tableView.reloadData()
my keyboard get closed.

I've tried the code below but I get this error
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 5 from section 2 which only contains 4 rows before the update'
So please where would be my issue?

let newCount = self.placeArray.count

var newIndexPaths = NSMutableArray(capacity: newCount)

for var i = 0 ; i < newCount ; i++ {
var indexPath = NSIndexPath(forRow: i, inSection: 2)
newIndexPaths.addObject(indexPath)
}

self.tableView.beginUpdates()
self.tableView.reloadRowsAtIndexPaths(newIndexPaths as [AnyObject], withRowAnimation: UITableViewRowAnimation.None)
self.tableView.endUpdates()

Answer

You could use UITableView's reloadSections method instead.

tableView.reloadSections(NSIndexSet(index: 2), withRowAnimation: .None)