将新单元格插入 UICollectionView

Inserting a New Cell Into A UICollectionView

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

    - (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;

}

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

*** 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).'

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

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

不要修改collectionView:cellForItemAtIndexPath:中的数据源。 Return - collectionView:numberOfItemsInSection: 中的不同数量的项目改为:

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

collectionView:cellForItemAtIndexPath: 中,您应该 return 用于 "normal" 项的 "normal" 单元格,以及用于额外一项的 "extra" 单元格,具体取决于indexPath.row。例如:

- (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
    }
}