8

UITableView custom cell update delay

 2 years ago
source link: https://www.codesd.com/item/uitableview-custom-cell-update-delay.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

UITableView custom cell update delay

advertisements

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.


You may need to call reloadRowsAtIndexPaths once the data has been loaded. What could be happening is that the cell data has been loaded but the cell drawing is not updated.

Also I believe your code can request the same data multiple times because if [e infoDetails] is nil a request is made, but the cell could be requested multiple times before the data is loaded so [self startInfoDownload:e forIndexPath:indexPath] would be called multiple times, downloading the same data. You should look into keeping track of which rows you have requested data for.

Check out this code for some ideas on how to solve this: https://github.com/kgn/Spectttator/blob/master/SpectttatorTest-iOS/RootViewController.m#L123


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK