1

I have issue with redrawing images of uicollectionview cells - images are downloaded from URL parsed from JSON. I have 10 cells and with the start of app only first 6 are displayed, but images are not loaded, when I scroll down to those other cells, they have their images, and when I scroll back to the top, first 4 cells have their images as well, only cells 5 and 6 are not redrawn(they were visible for the whole time). I've been trying to debug this for long time but no success.

in cellForItemAtIndexPath I'm calling SDWebImage(but it doesn't really matter):

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

but when cellForItemAtIndexPath is called for the first time, timeSnap.thumbnailImageURL(where timeSnap entity is model for cell) is not initialised yet and so it's null

after initialisation of timeSnap.thumbnailImageURL I have:

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

edit1: adding my code, it's a bit complex - first I'll get JSON from server API with items to be displayed in collection view, then for each item I have to get another JSON from server API, from which I can build URL for timeSnap.thumbnailImageURL

viewController class:

- (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;
}

adapter class:

- (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);
        }
    });    
}
4

2 回答 2

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这里使用(除非您不考虑适配器合同的那部分)。

于 2013-11-09T16:39:39.383 回答
0

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

dispatch_async(dispatch_get_main_queue(), ^{
    [cell.backgroundImage setImageWithURL:[NSURL URLWithString:timeSnap.thumbnailImageURL]  placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
});
于 2013-11-09T02:30:35.527 回答