UITableView animated reload causes each cell to load immediately

I have a table view with 100 cells. At first it is empty. Then I call:

[_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationLeft]; 

Since there was nothing in the table view, I need to create several cells. However, I expected the table view to be set to 10 (to fit the screen size) ... not 100!

This does not happen when I just reload the table view without any animation:

 [_tableView reloadData]; 

This problem forces the table to reload very slowly: is there a way to make it query only 10 cells?


Edit

Perhaps I was not clear enough: At first , the table has no record . Then , I add 100 records to my data source and ask to reload the table: there is no visible cell before the reboot, so the reloadRowsAtIndexPaths solution reloadRowsAtIndexPaths not work.

+4
source share
4 answers

I found out that this problem only occurs on iOS 5.1 and below. No need to register a bug, as it is fixed in iOS 6. Thanks for your answers anyway!

0
source

It looks like you are inserting new rows into the table and not reloading, so why not use:

 [_tableView insertRowsAtIndexPaths:... withRowAnimation:...]; 

You may need to insert the section first:

 [_tableView insertSections:... withRowAnimation:..]; 

http://developer.apple.com/library/ios/ipad/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

+2
source

If you want to reload, say, 10 cells, the following code will work.

  int cellsToReload = 10; NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:cellsToReload]; for(int x = 0; x < cellsToReload; x++) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:x inSection:0]; [indexPaths addObject:indexPath]; } [self.theTableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft]; 

In the code above, when you reloaded the entire column, you reloaded the entire section, not just a few rows. I assume that your table has only one section.

0
source

NSArray *paths = [_tableView indexPathsForVisibleRows];
[_tableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];
will do exactly what you need - to reload only visible raws, no more, no less.

0
source

Source: https://habr.com/ru/post/1436992/


All Articles