更新后集合视图中包含的节数必须等于更新前的节数

时间:2013-10-07 11:21:17

标签: ios uicollectionview uicollectionviewcell

我有一个包含10个部分的UICollectionView,每个部分有2个单元格。 我试图删除一个单元格,但我得到了这个:

The number of sections contained in the collection view after the update (2) must be equal to the number of sections contained in the collection view before the update (2)

这是我删除的方式:

NSUInteger arrayLength = 1;
    NSUInteger integerArray[] = {0,0};
    NSIndexPath *aPath = [[NSIndexPath alloc] initWithIndexes:integerArray length:arrayLength];
    [self.collectionFavs deleteItemsAtIndexPaths:[NSArray arrayWithObject:aPath]];

我只想删除包含单元格的单元格或部分。我的错是什么?

1 个答案:

答案 0 :(得分:3)

您应该从数据源数组中删除元素。 F.E.如果你的方法在删除单元格之前返回2,那么在删除之后它应该返回1

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (notdeleted)
        return 2;
    else
        return 1;
}

实际上如果你在这个方法中返回[array count],那么你应该在调用-deleteItemsAtIndexPaths之前从数组中删除1个对象

NSUInteger integerArray[] = {0,0};
NSIndexPath *aPath = [[NSIndexPath alloc] initWithIndexes:integerArray length:arrayLength];
[array removeObjectAtIndex:0];
[self.collectionFavs deleteItemsAtIndexPaths:[NSArray arrayWithObject:aPath]];