UITableView reloads old images

I know this has been asked many times, but please bear with me as I tried for several days to find a solution.

I have an rss parser, a tableview with: text, details and images set in AccessoryDisclosureIndicator.view.

Images are uploaded using the simple async GCD: fast scrolling for hundreds of results. NO lag, NO errors if I have a good connection.

The problem is that for a split second - they flicker when loading, because the cell is being reused. In addition, if the connection is poor, it sometimes leaves a hinged image, but the text / detail is correct, only the image is out of date ... So let me repeat, the text / details are updated perfectly and NEVER make a mistake, just the image sometimes rarely gets queued incorrectly with poor connections / grip scrolling forward and backward.

My question is, can someone help me cache / tag cell.accs.views? I tried installing cellIDs but had problems with my implementation. My code below works fine if the connection never slows down, just a little flicker when the cell reloads, which I don't mind.

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

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    [cell.textLabel setNumberOfLines:3];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0];

    [cell.detailTextLabel setNumberOfLines:3];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:12.0];
    cell.detailTextLabel.textColor = [UIColor blackColor];
}

RSSItem * rssItem = (RSSItem *)(_rssParser.rssItems)[indexPath.row];

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
dispatch_async(queue, ^{
    //This is what you will load lazily
    NSData   *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:rssItem.imageURL]];
    dispatch_sync(dispatch_get_main_queue(), ^{

        UIImageView *accImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data ]];

        [accImageView setFrame:CGRectMake(0, 0, accImageView.image.size.width, 92)];
        cell.accessoryView = accImageView;

        //[cell setNeedsLayout];//not needed
    });
});

[cell.textLabel setText:rssItem.title];
[cell.detailTextLabel setText:rssItem.summary];

return cell;}
+5
source share
4

SDWebImage, , , , . , . .

RSSItem * rssItem = (RSSItem *)(_rssParser.rssItems)[indexPath.row];

if (rssItem.imageURL == nil ){
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    [imageView setImageWithURL:[NSURL URLWithString:rssItem.imageURL] placeholderImage:[UIImage imageNamed:@"chevron.png"]];
    cell.accessoryView = imageView;
}else{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 110, 96)];
    imageView.contentMode  = UIViewContentModeScaleAspectFit;
    [imageView setImageWithURL:[NSURL URLWithString:rssItem.imageURL] placeholderImage:[UIImage imageNamed:@"loading.png"]];
    cell.accessoryView = imageView;
}

[cell.textLabel setText:rssItem.title];
[cell.detailTextLabel setText:rssItem.summary];

return cell;
0

, , dispatch_async (, , , ), reset -.

:

  • , , , (, , , , , ). , , .

    , ( ), :

    dispatch_async(dispatch_get_main_queue(), ^{
        UITableViewCell *updateCell = [tableView cellForRowAtIndexPath:indexPath];
        if (updateCell != nil) {
            // update image in `updateCell`
        }
    });
    

    , UITableView cellForRowAtIndexPath UITableViewDataSource, tableView:cellForRowAtIndexPath:. , , , . , , , , .

    , , NSIndexPath , , , . , , , indexPath , , , .

  • , , , . , () 4 5 ; () , iOS. 4 5 ( , , , , -). NSOperationQueue, maxConcurrentOperationCount .

    , , , NSOperation (, , , ). , 100- , , , 99 . , , .

, Network Conditioner, Mac : Edge , Xcode Xcode " ...", " ". "Hardware IO Tools for Xcode". Network Conditioner . , , , , ..

, , , - , . UIImageView, , : , , - SDWebImage AFNetworking.

+10

.

:

cell.accessoryView = nil;
+1

, - ,

. , , . ( ), cell.accessoryView ( ). (dispatch_async).

, , . , , , . , , , :

http://www.apeth.com/iOSBook/ch37.html#_http_requests

, : ", , , UITableView. , ".

+1

All Articles