Inserting a New Cell Into A UICollectionView

2019-08-11 10:29发布

问题:

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:

*** 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?

回答1:

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

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