5

I need implement some kind of lazy loading in my UITableView.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //... some initializations here

    //this is a function that pulls my cell data using helper class
    NSData *cellData=[MyDataClass dataForCellAtIndexPath:indexpath.row];

    //... here I populate pulled data to cell

    return cell;
}

Everything works great, but table view scrolls not smoothly, because dataForCellAtIndexPath method is slow. So I need to implement lazy populating data into cells by calling this method. The result I expect is that table view will scroll smoothly but cell's content will populate a bit after the cell is drawn. Help me please, How can it be done?

Oleg
  • 1,383
  • 4
  • 19
  • 35

3 Answers3

4

Yes, you could look at pushing your data collection onto a background thread. The first thing to try is the simplest option to see if it improves anything:

[MyDataClass performSelectorInBackground:@selector(dataForCellAtIndexPath:) withObject:indexpath.row];

This post mentions some of the other, more complex, customisable options like Grand Central Dispatch and NSThread.

Community
  • 1
  • 1
  • What will it improve? If cell needs to be drawn it should be drawn immidiately. So calling slow method in every tread will result nothing. But thanks for references – Oleg Apr 11 '12 at 05:30
  • @Oleg It would allow the cells to be drawn immediately and their content filled in afterwards. – GregularExpressions Apr 11 '12 at 07:24
  • Result: table view scrolls normally, because it is not waiting intil request in background will be finished so it populates the cells with nil data. The `dataForCellAtIndexPath` method (some kind of) returns nil at the period of time when cell is needs to be populated with data. – Oleg Apr 11 '12 at 12:24
  • @Oleg It sounds like your data method doesn't know which cell it should be applying the data to so nothing happens? Consider passing a reference to the cell through as an argument to the data method so it knows which one to update. Failing that you could call cellForRowAtIndexPath again as you've got the indexPath. – GregularExpressions Apr 11 '12 at 13:22
2

If you are using a large amount of data, I would suggest saving them onto Core-data and/or use NSFetchedResultsController to populate your tableView.

It has been created & tied up with your tableView mainly to populate your tableView faster and have high performance.

Here is the link to the documentation of NSFetchedResultsController !

Legolas
  • 12,145
  • 12
  • 79
  • 132
-1

for that u can load your new data on the basis of index value of table cells

you can put condition for how much new data you want to load because its depends on which cell s are currently examine by the users.

freelancer
  • 1,658
  • 1
  • 16
  • 37