Tuesday, April 10, 2012

Delay when refreshing UITableView custom cell

I have a UITableView with a custom cell which i fill (with array infoservices) after parsing the xml data.



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[self.cellNib instantiateWithOwner:self options:nil];
cell = tmpCell;
self.tmpCell = nil;
}

infoService *e = [self.infoservices objectAtIndex:indexPath.row];

cell.name = [e infoName];

NSString *infodetails = [e infoDetails];

if ( infodetails == nil ) {
cell.details = @"Loading...";
[self startInfoDownload:e forIndexPath:indexPath];

NSLog(@"Loading...");
} else {
cell.details = infodetails;
NSLog(@"Show info detail: %@", infodetails );
}
return cell;
}


- (void)infoDidFinishLoading:(NSIndexPath *)indexPath
{
infoDownloader *infoserv = [imageDownloadsInProgress objectForKey:indexPath];
if (infoserv != nil)
{

[infoservices replaceObjectAtIndex:[indexPath row] withObject:infoserv.appRecord];

NSIndexPath *a = [NSIndexPath indexPathForRow:indexPath.row inSection:0]; // I wanted to update this cell specifically
ApplicationCell *cell = (ApplicationCell *)[self.tableView cellForRowAtIndexPath:a];
cell.details = [[infoservices objectAtIndex:[indexPath row]] infoDetails];

NSLog(@"Updating=%@", [[infoservices objectAtIndex:[indexPath row]] infoDetails]);
}
}


For each cell i'm using NSURLConnection sendAsynchronousRequest to retrieve and parse xml data from object infoDownloader with



- (void)startDownload


for each individual cell.



After data has been successfully parsed delegate method from infoDownloader is called



- (void)infoDidFinishLoading:(NSIndexPath *)indexPath


The problem is that, while the



- (void)infoDidFinishLoading:(NSIndexPath *)indexPath 


gets called after parsing each cell and i can see the



NSLog(@"Updating=%@", [[infoservices objectAtIndex:[indexPath row]] infoDetails]);


in the debugger with the correct details, the cell does not get refreshed immediately but after 6 or 7 seconds. Also cellForRowAtIndexPath does not get called from



- (void)infoDidFinishLoading:(NSIndexPath *)indexPath 


for some reason because there is not debug output after the infoDidFinishLoading. Also i don't understand how the cell.details gets actually refreshed since cellForRowAtIndexPath isn't called again.



I've tried to setup this function using Apple's LazyTableImages loading example, which i have used successful, but i don't know what's going wrong.





No comments:

Post a Comment