【问题标题】:reload issue with UICollectionView iOS SDKUICollectionView iOS SDK 的重新加载问题
【发布时间】:2015-02-06 05:03:04
【问题描述】:

大家好,我正在显示来自网络服务的图像,现在我的问题是我遇到了一些不知道的奇怪问题

这是我的代码 sn-p

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


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

NSString *strurl=[[arrUrlImage objectAtIndex:indexPath.row] objectAtIndex:0];

NSURL *url = [NSURL URLWithString:strurl];


ImageRequest *request = [[ImageRequest alloc] initWithURL:url];
cell.imageView.image=[UIImage imageNamed:@"purple.jpg"];
UIImage *image = [request cachedResult];
if (image) { 

    cell.imageView.image=image;       

} else {
    [request startWithCompletion:^(UIImage *image, NSError *error) {
        if (image && [[collectionView indexPathsForVisibleItems] containsObject:indexPath]) {






            @try
            {
                [_collectionView performBatchUpdates:^{                       

                    [_collectionView reloadItemsAtIndexPaths:@[indexPath]];
                } completion:nil];

            }
            @catch (NSException *except)
            {                    
                NSLog(@"%@", except.description);
            }

        }
    }];
}


if (indexPath.row==arrUrlImage.count-1) {
    NSLog(@"load more image.");
    NSString *nextPageUrl=[[arrMedia valueForKey:@"pagination"] valueForKey:@"next_url"];
    [arrMedia removeAllObjects];
    NSLog(@"%@",nextPageUrl);
    ShowNetworkActivityIndicator();
    [self grabMedia:nextPageUrl];     

}

return cell;  
}

我收到此错误消息的异常

Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit_Sim/UIKit-2903.23/UICollectionView.m:3716

和异常日志:无效更新:第0节中的项目数无效。更新后现有节中包含的项目数(80)必须等于更新前该节中包含的项目数( 60),加上或减去从该部分插入或删除的项目数(1 个插入,1 个删除),加上或减去移入或移出该部分的项目数(0 移入,0 移出)。

任何帮助。

【问题讨论】:

  • 只需尝试在_collectionView上注释您执行批量更新的行

标签: ios xcode uikit uicollectionview


【解决方案1】:

您正在调用reloadItemsAtIndexPaths,而实际上不存在单元格,因此您需要使用insertItemsAtIndexPaths

[_collectionView performBatchUpdates:^{                       

                [_collectionView insertItemsAtIndexPaths:@[indexPath]];
            } completion:nil];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2014-10-31
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    相关资源
    最近更新 更多