I have UITableView
. in tableView:cellForRow:atIndexPath:
method (when data populates to cells) I've implemented some kind of lazy loading. If there's no object for key(key==row number) in rowData
NSDictionary
program launches requestDataForRow:
method in background. so the data in cells gets populated a bit after the cell becomes visible.
Here's the code:
static int requestCounter=0;
-(void)requestDataForRow:(NSNumber *)rowIndex
{
requestCounter++;
//NSLog(@"requestDataForRow: %i", [rowIndex intValue]);
PipeListHeavyCellData *cellData=[Database pipeListHeavyCellDataWithJobID:self.jobID andDatabaseIndex:rowIndex];
[rowData setObject:cellData forKey:[NSString stringWithFormat:@"%i", [rowIndex intValue]]];
requestCounter--;
NSLog(@"cellData.number: %@", cellData.number);
if (requestCounter==0)
{
//NSLog(@"reloading pipe table view...");
[self.pipeTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
};
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"pipeCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
[[NSBundle mainBundle] loadNibNamed:@"PipesForJobCell" owner:self options:nil];
cell = pipeCell;
self.pipeCell = nil;
PipeListHeavyCellData *cellData=[[PipeListHeavyCellData alloc] init];
if ([rowData objectForKey:[NSString stringWithFormat:@"%i", indexPath.row]]==nil)
{
//NSLog(@" nil data for row: %i", indexPath.row);
[self performSelectorInBackground:@selector(requestDataForRow:) withObject:[NSNumber numberWithInt:indexPath.row]];
}
else
{
//NSLog(@" has data for row: %i", indexPath.row);
PipeListHeavyCellData *heavyData=[[PipeListHeavyCellData alloc] init];
heavyData=(PipeListHeavyCellData *)[rowData objectForKey:[NSString stringWithFormat:@"%i", indexPath.row]];
cellData._id=[heavyData._id copy];
cellData.number=[heavyData.number copy];
cellData.status=[heavyData.status copy];
};
This code works, everything is OK, BUT my table has 2000 rows and If users scrolls from cell with index 10 to cell with index 2000 very quickly. He must wait for a long time until all pulling data requests will complete (for rows 11, 12, 13, ..., 2000) cause that rows became visible while user was scrolling table view so the method requestDataForRow
was called for them.
How can I optimize those things?