ios - UICollectionview 单元格重绘错误

标签 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 实体是单元模型)尚未初始化,因此它为空

在 timeSnap.thumbnailImageURL 初始化后我有:

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

编辑1: 添加我的代码,它有点复杂 - 首先我将从服务器 API 获取 JSON,其中包含要在 Collection View 中显示的项目,然后对于每个项目,我必须从服务器 API 获取另一个 JSON,我可以从中为 timeSnap 构建 URL。缩略图图片网址

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

最佳答案

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

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

关于ios - UICollectionview 单元格重绘错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19869527/

相关文章:

css - 如何设置图像编辑页面

ios - 使用 Swift 在 iOS 中获取 wifi 的扫描 SSID 列表

ios - 如何保存应用程序状态并再次恢复它

ios - 弹出广告有效,但不是在我实现应用内购买后将其删除?

ios - 为什么应用程序会在设备内存不足时崩溃?

javascript - 建议的背景网页正确图片尺寸

ios - 为什么我的应用程序中的scrollView 方法不起作用?

objective-c - Objective-C 的 IoC 容器

ios - FBAnnotationClustering 以编程方式显示注释

cocoa - 在 PyObjC 中的 NSImage 上绘制文本时出错