UICollectionview单元格错误重绘

时间:2013-11-08 22:17:36

标签: ios objective-c image uicollectionview

我遇到了重绘uicollectionview单元格图像的问题 - 从JSON解析的URL下载图像。我有10个单元格,只有前6个应用程序的开始显示,但是没有加载图像,当我向下滚动到其他单元格时,它们有图像,当我滚动回到顶部时,前4个单元格有它们的图像也是如此,只有细胞5和6没有被重绘(它们在整个时间内是可见的)。我一直试图调试这个很长一段时间但没有成功。

在cellForItemAtIndexPath中的

我正在调用SDWebImage(但这并不重要):

[cell.backgroundImage setImageWithURL:[NSURL URLWithString:timeSnap.thumbnailImageURL]  placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

但是第一次调用cellForItemAtIndexPath时,timeSnap.thumbnailImageURL(其中timeSnap实体是单元格的模型)尚未初始化,因此它为null

在初始化timeSnap.thumbnailImageURL后,我有:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.collectionView reloadData];
});

EDIT1: 添加我的代码,它有点复杂 - 首先我将从服务器API获取JSON,其中项目将显示在集合视图中,然后对于每个项目,我必须从服务器API获取另一个JSON,我可以从中为timeSnap构建URL。 thumbnailImageURL

viewController类:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self refreshNewTimeSnapsFeed];
}

- (void)refreshNewTimeSnapsFeed {
    Adapter *adapter = [AppDelegate instance].adapter;

    [adapter getNewTimeSnapsWithCompletion:^(NSArray* jsonResponse) {
        [self newTimeSnapsFeedRefreshed:jsonResponse];
    }];
}

- (void)newTimeSnapsFeedRefreshed:(NSArray*)jsonResponse {
Adapter *adapter = [AppDelegate instance].adapter;

    for (NSDictionary *timeSnapDict in jsonResponse) {
        TimeSnap *timeSnap = [[TimeSnap alloc] initWithJSON:timeSnapDict];
        [adapter getTimeSnapSnapsforId:timeSnap.id withCompletion:^(NSArray*     jsonResponse) {
            [timeSnap getSnapsJSON:jsonResponse];
            [timeSnap getTimeSnapThumbnailImageURL];
        }];

        [self.timeSnaps addObject:timeSnap];
    }

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.collectionView reloadData];
    });
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    TimeSnapCell *cell = (TimeSnapCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"TimeSnapCell" forIndexPath:indexPath];

    TimeSnap *timeSnap = self.timeSnaps[indexPath.row];

    cell.label.text = timeSnap.name;

    [cell.backgroundImage setImageWithURL:[NSURL URLWithString:timeSnap.thumbnailImageURL]  placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    return cell;
}

适配器类:

- (void)getNewTimeSnapsWithCompletion:(void (^)(NSArray* jsonResponse))completion {

    NSString *urlAsString = [NSString stringWithFormat:@"..."];
    NSURL *url = [[NSURL alloc] initWithString:urlAsString];

    [self getJSONWithCompletion:completion fromURL:url forKey:@"Items"];    
}

- (void)getTimeSnapSnapsforId:(NSNumber*)id withCompletion:(void (^)(NSArray* jsonResponse))completion {

    NSString *urlAsString = [NSString stringWithFormat:@"..."];
    NSURL *url = [[NSURL alloc] initWithString:urlAsString];

    [self getJSONWithCompletion:completion fromURL:url forKey:@"Items"];    
}

- (void)getJSONWithCompletion:(void (^)(NSArray* jsonResponse))completion fromURL:(NSURL*)url forKey:(NSString*)key {

    //code for getting JSON from server API, then parsing into NSArray *items

    dispatch_async(dispatch_get_main_queue(), ^{
        if(completion){
            completion(items);
        }
    });    
}

2 个答案:

答案 0 :(得分:1)

当第二个JSON响应返回时,您需要重新加载单元格:

for (NSDictionary *timeSnapDict in jsonResponse) {
    TimeSnap *timeSnap = [[TimeSnap alloc] initWithJSON:timeSnapDict];

    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.timeSnaps.count inSection:0];
    [self.timeSnaps addObject:timeSnap];
    [adapter getTimeSnapSnapsforId:timeSnap.id withCompletion:^(NSArray*     jsonResponse) {
        [timeSnap getSnapsJSON:jsonResponse];
        [timeSnap getTimeSnapThumbnailImageURL];
        [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
    }];
}

您的适配器类在主队列上执行完成块,因此您不需要在此处使用dispatch_async(除非您不考虑适配器合同的那部分)。

答案 1 :(得分:0)

将调度删除到reloadData中,并删除reloadData并仅添加此代码

dispatch_async(dispatch_get_main_queue(), ^{
    [cell.backgroundImage setImageWithURL:[NSURL URLWithString:timeSnap.thumbnailImageURL]  placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
});