问题
Why does UITableView take more time to load when the number of rows is large? What is it that takes time between viewDidLoad and the first cellForRowAtIndexPath function call?
These are the log statements when numberOfRows returns 100,000 (notice the time (5 seconds delay)):
2014-02-05 20:51:22.806 TableViewTest[3995:60b] View Did Load
2014-02-05 20:51:27.526 TableViewTest[3995:60b] Cell for row at indexpath.row: 0
These are the log statements when numberOfRows returns 10,000 (notice the time (1 second delay)):
2014-02-05 20:54:50.793 TableViewTest[4007:60b] View Did Load
2014-02-05 20:54:51.846 TableViewTest[4007:60b] Cell for row at indexpath.row: 0
回答1:
If you implement heightForRowAtIndexPath:
in your table view, then iOS will go and check the height of each cell, I think to draw the scroll bar. So it's worth it to make that call very fast if possible.
If you are supporting iOS 7, then consider implementing tableView: estimatedHeightForRowAtIndexPath:
which can just return a constant (or maybe very quick logic) to guess at the height. This will delay when heightForRowAtIndexPath:
is called until right before the cell is actually viewed and should speed things up a lot.
来源:https://stackoverflow.com/questions/21577976/uitableview-takes-much-longer-to-load-when-numberofrows-returns-a-large-number