本文介绍了一次加载一个 UICollectionViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这篇文章 http://aplus.rs/2014/how-to-animate-in-uicollectionview-items/ 我试图一次加载一个 UICollectionViewCell 以在单元格出现时为其设置动画.但是,当我调用循环 cellCount++ 的代码时,它会产生此错误:

由于未捕获的异常而终止应用'NSInternalInconsistencyException',原因:'无效更新:无效第 0 部分中的项目数.更新后的现有部分 (1) 必须等于更新前包含在该部分中的项目 (1),加上或减去从该部分插入或删除的项目数(插入 1 个,0 已删除)并加上或减去移入或移出的项目数该部分(0 移入,0 移出).'

我终其一生都无法弄清楚.

这是我的代码:

-(void)addCells{for (self.cellCount=0; self.cellCount
解决方案

您可以使用计时器来执行此操作,计时器的操作方法将对象从数据源数组添加到用于填充集合视图的可变数组.

>

-(void)viewDidLoad {[超级viewDidLoad];self.mutableArray = [NSMutableArray new];self.data = @[@"one", @"one", @"one", @"one", @"one", @"one"];[NSTimer scheduleTimerWithTimeInterval:.1 target:self selector:@selector(addCells:) userInfo:nil repeats:YES];}-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {返回 self.mutableArray.count;}-(void)addCells:(NSTimer *) timer {静态整数计数器 = 0;[self.mutableArray addObject:self.data[counter]];计数器++;[self.collectionview insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:self.mutableArray.count -1 inSection:0]]];if (self.mutableArray.count == self.data.count) {[定时器失效];}}

As per this article http://aplus.rs/2014/how-to-animate-in-uicollectionview-items/ I am trying to load a UICollectionViewCell one at a time to animate the cell when it appears. However, when I call the code the loops the cellCount++, it produces this error:

And I cannot for the life of me figure it out.

Here is my code:

-(void)addCells{

    for (self.cellCount=0; self.cellCount<50; self.cellCount++) {

        [self.collectionView performBatchUpdates:^{
            [self.collectionView insertItemsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForItem:self.cellCount inSection:0]]];
        } completion:nil];

    }

}
解决方案

You can do this using a timer, whose action method adds objects from your data source array to a mutable array that you use to populate your collection view.

-(void)viewDidLoad {
    [super viewDidLoad];
    self.mutableArray = [NSMutableArray new];
    self.data = @[@"one", @"one", @"one", @"one", @"one", @"one"];
    [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(addCells:) userInfo:nil repeats:YES];
}

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


-(void)addCells:(NSTimer *) timer {
    static int counter = 0;
    [self.mutableArray addObject:self.data[counter]];
    counter ++;
    [self.collectionview insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:self.mutableArray.count -1 inSection:0]]];
    if (self.mutableArray.count == self.data.count) {
        [timer invalidate];
    }
}

这篇关于一次加载一个 UICollectionViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:47