BigFire
BigFire

Reputation: 337

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

Upvotes: 1

Views: 4390

Answers (2)

rmaddy
rmaddy

Reputation: 318854

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)

Upvotes: 10

Shehata Gamal
Shehata Gamal

Reputation: 100533

Assuming the first visible cell is the 1 on the most top You can do

 self.tableView.reloadRows(at: Array(visibleIndex.dropFirst()), with: .automatic)

Or if not

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

==

let visibleIndex:[IndexPath] = self.tableView.visibleCells.compactMap { item in
    let index = self.tableView.indexPath(for:item)!
    return index.row != 0 ? index : nil
}

Upvotes: 3

Related Questions