Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollection视图单元格重绘错误_Ios_Objective C_Image_Uicollectionview - Fatal编程技术网

Ios UICollection视图单元格重绘错误

Ios UICollection视图单元格重绘错误,ios,objective-c,image,uicollectionview,Ios,Objective C,Image,Uicollectionview,我对重新绘制uicollectionview单元格的图像有意见-图像是从URL下载的,从JSON解析而来。我有10个单元格,在应用程序启动时,只显示前6个单元格,但没有加载图像,当我向下滚动到其他单元格时,它们有图像,当我向后滚动到顶部时,前4个单元格也有图像,只有单元格5和6没有被重绘(它们在整个时间内都可见)。我一直在尝试调试这个很长时间,但没有成功 在cellForItemAtIndexPath中,我调用SDWebImage(但这并不重要): 但当第一次调用cellForItemAtInd

我对重新绘制uicollectionview单元格的图像有意见-图像是从URL下载的,从JSON解析而来。我有10个单元格,在应用程序启动时,只显示前6个单元格,但没有加载图像,当我向下滚动到其他单元格时,它们有图像,当我向后滚动到顶部时,前4个单元格也有图像,只有单元格5和6没有被重绘(它们在整个时间内都可见)。我一直在尝试调试这个很长时间,但没有成功

在cellForItemAtIndexPath中,我调用SDWebImage(但这并不重要):

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

初始化timeSnap.thumbnailImageURL后,我有:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.collectionView reloadData];
});
edit1: 添加我的代码有点复杂-首先,我将从服务器API获取JSON,其中包含要在集合视图中显示的项,然后对于每个项,我必须从服务器API获取另一个JSON,从中我可以构建timeSnap.thumbnailImageURL的URL

视图控制器类:

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

删除reloadData中的分派,同时删除reloadData并仅添加以下代码

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

删除reloadData中的分派,同时删除reloadData并仅添加以下代码

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

当第二个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]];
    }];
}

您的适配器类在主队列上执行完成块,因此您不需要使用<代码> DeXCHOYASYNC/<代码>(除非您不考虑适配器的合同的一部分).< /P> < P>当第二个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]];
    }];
}

您的适配器类在主队列上执行完成块,因此您不需要使用<代码> DeXCHCHYASYNC <代码>(除非您不考虑适配器的合同的一部分)。请提供代码。请查看它。提供代码。请查看IITI,谢谢您的回复。我正在使用SDWebImage库进行图像下载和缓存,它负责发送本身,如果我将发送删除到reloadData和reloadData中,则不会显示任何内容,我将提供更清晰的代码嗨,感谢您的回复,我正在使用SDWebImage库进行图像下载和缓存,它负责调度本身,如果我将调度删除到reloadData和reloadData中,则不会显示任何内容,我将提供代码以使其更清晰