Images in UITableView keep re-loading and wrong images flash while scrolling

前端 未结 6 1356
醉酒成梦
醉酒成梦 2021-01-16 19:40

i have created a UITableView which populates each cell from URL requests. I have used \'dispatch_queue\' to prevent the UItableView from freezing. For some reason when i s

6条回答
  •  执念已碎
    2021-01-16 20:26

    your code was missing one line , that i added.

    customCell.customCellImageView.image =nil;

    customCell.customCellTextLabel.text = [NSString stringWithFormat:@"%@",feedO.title];
    
    NSString *string = [NSString stringWithFormat:@"%@",feedO.body];
    
    NSURL *urlString;
    customCell.customCellImageView.image =nil;
    
        NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
        NSArray *matches = [linkDetector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
        for (NSTextCheckingResult *match in matches) {
            if ([match resultType] == NSTextCheckingTypeLink) {
                urlString = [match URL];
                NSLog(@"found Body URL: %@ and title %@", urlString,feedO.title);
            }
        }
    
        dispatch_queue_t imageQueue = dispatch_queue_create("imageDownloader",nil);
        dispatch_async(imageQueue, ^{
    
            NSData *data = [[NSData alloc] initWithContentsOfURL:urlString];
    
            dispatch_async(dispatch_get_main_queue(), ^{
    
                customCell.customCellImageView.image = [UIImage imageWithData: data];
            });
        });
    
    
    return customCell;
    

提交回复
热议问题