20

Is that possible? To reload only the cells that are visible in order to do it more efficient.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Samui
  • 1,384
  • 3
  • 14
  • 28

3 Answers3

32

That's how table views work by default. The UITableView class never loads cells until they're about to appear onscreen, even when you call reloadData.

jlehr
  • 15,557
  • 5
  • 43
  • 45
  • jlehr@can you provide any reference – Rohit Pradhan Mar 10 '16 at 05:23
  • There will be as many cells loaded (and created) which intersect with the scroll view's frame, even when portions of the frame are not visible. This of course only happens in certain scenarios where the table view is itself a subview of another scroll view and where the frame's size would match its content size and scrolling would be disabled for the table view and achieved by the embedding scroll view. – CouchDeveloper Aug 02 '18 at 19:28
  • This is outdated as of iOS 10 due to cell prefetching. Post iOS 10 `cellForRow` is called slightly before the cell will be visible. – Caleb Friden Apr 16 '20 at 19:37
  • No, it is a wrong answer.You can't use `reloadData` in a lot of cases. For example if you insert/delete rows with animation `reloadata` breaks this animation – Vyachaslav Gerchicov Mar 11 '21 at 07:35
16

To reload visible cells of tableview in Swift 3.0:

guard let visibleRows = pTableView.indexPathsForVisibleRows else { return }

pTableView.beginUpdates()

pTableView.reloadRows(at: visibleRows, with: .none)

pTableView.endUpdates()
sweetfa
  • 5,457
  • 2
  • 48
  • 62
Usman Nisar
  • 3,031
  • 33
  • 41
  • 1
    This is the correct answer. But please guard indexPathsForVisibleRows against nil for empty tableViews. – iDoc Sep 30 '19 at 20:18
14

Try this, You can reload any cell of the tableview using reloadRowsAtIndexPaths:

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows]
                 withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
Kaey
  • 4,615
  • 1
  • 14
  • 18
  • I found only following places can use 'reloadRowsAtIndexPaths': - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath - (IBAction) unwindToMealList: (UIStoryboardSegue *) sender – Weidian Huang Oct 08 '16 at 06:35