I have a UITableView
. in the tableView:cellForRow:atIndexPath:
(when the data is filled with cells), I implemented some kind of lazy loading. If there is no object for the key in the rowData
NSDictionary
object (key number == line number), the requestDataForRow:
method is requestDataForRow:
in the background. therefore, the data in the cells is populated a little after the cell becomes visible. Here is 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 fine, BUT my table has 2000 rows and if users scroll very quickly from a cell with index 10 to a cell with index 2000. It should wait a long time until all data requests are completed (for rows 11, 12, 13, ..., 2000), because the rows became visible when the user scrolled the table view, so the requestDataForRow
method was called for requestDataForRow
.
How can I optimize these things?
source share