我试图将新单元格添加到我的集合视图中,仅当它已包含多个项目时才会添加。我没有太多的收集意见,并在文档和本网站的研究还没有帮助解决这个问题呢。所以,在我的cellForItemAtIndexPath方法,我做了检查,看看它是否填充。如果没有,我增加该小区,像这样:将新单元格插入到UICollectionView
- (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;
}
出于某种原因,我有一个断言被抛出,他说:
***终止应用程序由于未捕获的异常“NSInternalInconsistencyException”,原因:'无效更新:无效 部分0中的项目数。更新(7)后, 现有部分中包含的项目数量必须等于更新前(6)部分中包含的 项目数量 ,加号或减号 从该部分插入或删除的项目数(0 0删除)并加上或减去移入或移出 该项(0移入,0移出)的项目数。
当然,这个数字通常是变化的,但它总是对这个额外的细胞感到愤怒。一切都很好,直到我尝试并添加它。现在,对收集视图不熟悉,并在浏览本网站上的相关问题后,我决定是时候向专业人士提问。
有谁知道我应该如何改变这段代码才能完成我想要做的事情?
谢谢。我试过这个解决方案,但我碰到错误'NSRangeException',原因:*** - [__ NSArrayM objectAtIndex:]:索引6超越界限[0..5]' – John
你想'self.dataArray [ 'indexPath.row]'在'(indexPath.row
Kreiri
或者我如何更改我的代码以实现此目的?认为我正确理解了你,但不能有... – John