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()
You could use UITableView
's reloadSections
method instead.
tableView.reloadSections(IndexSet(integer: 2), with: .none)
Swift 3.0.1:
let sectionIndex = IndexSet(integer: 0)
tableView.reloadSections(sectionIndex, with: .none) // or fade, right, left, top, bottom, none, middle, automatic
Swift 3.1 Xcode 8.3.3 Answer :)
The problem is - each section includes header of course, so section is:
The answer is - reload rows directly by IndexPath. Example:
Let's say you've got 1 section in your table and 'array' of something you use as datasource for table:
let indexes = (0..<array.count).map { IndexPath(row: $0, section: 0) }
Now we've got array of indexes of cell we want to reload, so:
tableView.reloadRows(at: indexes, with: .fade)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With