How to create indexpaths for all rows and all sections for uitableview?
I made a UITableView
extension based on @Vakas answer. Also the sections and rows have to be checked for > 0
to prevent crashes for empty UITableView
s:
extension UITableView{
func getAllIndexes() -> [NSIndexPath] {
var indices = [NSIndexPath]()
let sections = self.numberOfSections
if sections > 0{
for s in 0...sections - 1 {
let rows = self.numberOfRowsInSection(s)
if rows > 0{
for r in 0...rows - 1{
let index = NSIndexPath(forRow: r, inSection: s)
indices.append(index)
}
}
}
}
return indices
}
}
Here is the solution in Swift 3
func getAllIndexPaths() -> [IndexPath] {
var indexPaths: [IndexPath] = []
// Assuming that tableView is your self.tableView defined somewhere
for i in 0..<tableView.numberOfSections {
for j in 0..<tableView.numberOfRows(inSection: i) {
indexPaths.append(IndexPath(row: j, section: i))
}
}
return indexPaths
}
You don't need reload all the rows. You only need to reload the visible cells (that is why indexPathsForVisibleRows
exists).
The cells that are off-screen will get their new data in cellForRowAtIndexPath:
once they become visible.