Search code examples
iosswiftuitableviewreloaddataindexpath

What's the most efficient way to reload all rows in a tableview except the first cell?


I have items in a tableview , and i want to reload or update the tableview data , except the first row or the first indexpath of the tableview.

 let visibleIndex = self.tableView.visibleCells.compactMap {  
 tableView.indexPath(for: $0)  }
  self.tableView.reloadRows(at: visibleIndex, with: .automatic)

However this reloads all the visible cells , how do i reload all the visible cells except the first row


Solution

  • No need to get the visible cells. Use indexPathsForVisibleRows and remove the index path for section 0, row 0.

    let allButFirst = (self.tableView.indexPathsForVisibleRows ?? []).filter { $0.section != 0 || $0.row != 0 }
    self.tableView.reloadRows(at: allButFirst, with: .automatic)