将新单元格插入 UICollectionView [英] Inserting a New Cell Into A UICollectionView

查看:29
本文介绍了将新单元格插入 UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个新单元格添加到我的集合视图中,前提是该单元格中已有多个项目.我对集合视图没有太多的工作,并且对文档和这个站点的研究还没有帮助解决这个问题.因此,在我的 cellForItemAtIndexPath 方法中,我会检查它是否已填充.如果没有,我添加单元格,如下所示:

I’m trying to add in a new cell into my collection view, only if it has more than one item already in it. I have no worked much with collection views, and research in the docs and this site had not helped solve this issue yet. So, on my cellForItemAtIndexPath method, I do a check to see if it is populated. If not, I add the cell, like so:

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.myArray.count != 0) {
        return self.myArray.count + 1;
    }
    else {
        return self.myArray.count;
    }
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    MyNormalCollectionViewCellS *cells = (MyNormalCollectionViewCells *) [collectionView dequeueReusableCellWithReuseIdentifier:@"MyNormalCollectionViewCells" forIndexPath:indexPath];
    cell.clipsToBounds = NO;
    DataClass *data = [self.myArray objectAtIndex:indexPath.row];
    [cells configureMyNormalCellsWith:data];

    if (0 < self.myArray.count) {

        UICollectionViewCell *deleteCell = [UICollectionViewCell new];
        deleteCell.backgroundColor = [UIColor yellowColor];
        NSArray *newData = [[NSArray alloc] initWithObjects:deleteCell, nil];

        [self.myArray addObjectsFromArray:newData];

        NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
        [self.myCollectionView insertItemsAtIndexPaths:arrayWithIndexPaths];

        return deleteCell;
    }

    return cell;

}

出于某种原因,我抛出了一个断言,声明:

For some reason, I have an assertion being thrown, stating:

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

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (7) must be equal to the number of items contained in that section before the update (6), plus or minus the number of items inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

当然,数字通常会有所不同,但它总是对额外的单元格感到愤怒.一切都很好,直到我尝试将其添加进来.现在,由于不熟悉集合视图并且在本网站上浏览了相关问题后,我决定是时候询问专业人士了.

Of course, the number usually varies, but it's always angry about that extra cell. Everything is fine up until I try and add it in. Now, being unfamiliar with collection views and after browsing related questions upon this site, I decided it was time to ask the pros.

有谁知道我应该如何更改此代码以完成我想要做的事情?

Does anyone know how I should change this code in order to accomplish what I'm trying to do?

推荐答案

不要修改 collectionView:cellForItemAtIndexPath: 中的数据源.在 - collectionView:numberOfItemsInSection: 中返回不同数量的项目:

Don't modify data source in collectionView:cellForItemAtIndexPath:. Return different number of items in - collectionView:numberOfItemsInSection: instead:

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section {
    if (self.dataArray.count > 0) { 
        return self.dataArray.count + 1; 
    }
    else { 
        return 0; 
    }
}

collectionView:cellForItemAtIndexPath: 中,根据 indexPath.row 的值,您应该为正常"项目返回正常"单元格,并为额外的项目返回额外"单元格.例如:

In collectionView:cellForItemAtIndexPath: you should return your "normal" cell for "normal" item, and "extra" cell for that extra one, depending on the value of indexPath.row. For example:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < self.dataArray.count) { // if indexPath.row is within data array bounds
         // dequeue, setup and return "normal" cell
    } else { // this is your "+1" cell
        // dequeue, setup and return "extra" cell
    }
}

这篇关于将新单元格插入 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆