相关疑难解决方法(0)

使用GCD异步下载UITableView的图像

我正在使用GCD异步下载我的uitableview的图像,但是有一个问题 - 当滚动图像时闪烁并且一直在变化.我尝试将每个单元格的图像设置为nil,但它没有多大帮助.快速向上滚动时,所有图像都是错误的.我该怎么办?这是我的细胞方法:

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

    if (self.loader.parsedData[indexPath.row] != nil)
    {
        cell.imageView.image = nil;
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
            dispatch_async(queue, ^(void) {

                NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self.loader.parsedData[indexPath.row] objectForKey:@"imageLR"]]];

                UIImage* image = [[UIImage alloc] initWithData:imageData];

                dispatch_async(dispatch_get_main_queue(), ^{
                    cell.imageView.image = image;
                    [cell setNeedsLayout];
                     });
            });

    cell.textLabel.text = [self.loader.parsedData[indexPath.row] objectForKey:@"id"];
    }
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

objective-c ios ios6

34
推荐指数
3
解决办法
5万
查看次数

标签 统计

ios ×1

ios6 ×1

objective-c ×1