UICollectionView insertItemsAtIndexPaths:引发错误

时间:2014-05-17 03:09:05

标签: ios uicollectionview

我知道有各种与此相关的问题,但没有一个答案回答了这个问题。

无论如何,我有一个UICollectionView应该永远滚动,但我似乎无法让insertItemsAtIndexPaths:工作。它引发了错误:

*** Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit/UIKit-2935.137/UICollectionView.m:3688

这是我的代码:

#import "ImageSliderViewController.h"

#define ITEM_CELL_IDENTIFIER @"ItemCell"

@interface ImageSliderViewController ()

@end

@implementation ImageSliderViewController

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - UICollectionView methods

-(void)setupCollectionView {
    self.items = [[NSMutableArray alloc] init];

    loadingGap = 5;

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

    [self.collectionView registerClass:[ImageSliderCell class] forCellWithReuseIdentifier:ITEM_CELL_IDENTIFIER];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [flowLayout setMinimumInteritemSpacing:0.0f];
    [flowLayout setMinimumLineSpacing:0.0f];
    [self.collectionView setPagingEnabled:YES];
    [self.collectionView setCollectionViewLayout:flowLayout];

    [self loadMorePosts];
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.items.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ImageSliderCell *cell = (ImageSliderCell *)[collectionView dequeueReusableCellWithReuseIdentifier:ITEM_CELL_IDENTIFIER forIndexPath:indexPath];

    cell.imageTitle.text = [NSString stringWithFormat:@"%ld",(long)indexPath.item];

    NSLog(@"%d : %d",indexPath.item,self.items.count-1);

    if (indexPath.item >= self.items.count-1) {
        [self loadMorePosts];
    }

    return cell;

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = self.collectionView.frame.size;
    size.height = size.height/2;
    return size;
}

- (void)loadMorePosts
{
    NSLog(@"Loading new items");
    NSUInteger loopTill = self.items.count + loadingGap;
    NSMutableArray *indexPathsToLoad = [NSMutableArray new];
    while (self.items.count < loopTill) {
        [indexPathsToLoad addObject:[NSIndexPath indexPathForItem:self.items.count inSection:0]];

        NSLog(@"Added section %d",self.items.count);

        AsyncImageView *img = [[DatabaseFetcher alloc] getPostWithID:[NSString stringWithFormat:@"%lu",(unsigned long)self.items.count]
                                                       inSection:@"features"
                                                       withFrame:CGRectMake(0, 0, self.collectionView.frame.size.width, self.collectionView.frame.size.height)];
        [self.items addObject:@[img]];
    };
    [self.collectionView reloadItemsAtIndexPaths:indexPathsToLoad];
}

@end

提前致谢!

更新:我更新了代码以使用reloadItemsAtIndexPaths,并根据建议进行了一些其他改进。谢谢!

2 个答案:

答案 0 :(得分:1)

[NSIndexPath indexPathForItem:self.items.count inSection:0]

应该是:

[NSIndexPath indexPathForItem:self.items.count - 1 inSection:0]

<强>更新

并且您应该通过在循环中构建NSMutableArray个索引路径并在最后插入它们(就像您已经完成并注释掉)一次提交所有插入。或者,您可以将循环插入performBatchUpdates块。

答案 1 :(得分:0)

感谢所有答案。

在另一个线程上尝试其中一个解决方案时,我看起来算错了。这是我的新解决方案:

-(void)addNewItems
{
    NSLog(@"Loading new items");
    NSMutableArray *indexPathsToLoad = [NSMutableArray new];
    for (int i = 0; i < LOADING_GAP; i++) {
        [indexPathsToLoad addObject:[NSIndexPath indexPathForItem:self.items.count inSection:0]];
        [self.items addObject:@"Test"];
    }
    if (self.items.count == LOADING_GAP) {
        [self.collectionView reloadData];
    } else {
        [self.collectionView insertItemsAtIndexPaths:indexPathsToLoad];
    }
}

谢谢!