【发布时间】:2013-12-05 23:03:27
【问题描述】:
我有一个简单的集合视图,它有两种类型的单元格。一个计数器单元和一个“添加”单元。我的目标是在添加新对象后让“添加”单元格保留在索引的末尾。当我运行应用程序时,会出现我的“添加”单元格,但按下“添加”按钮时出现错误。
这是错误:
由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'无效更新:无效 第 0 节中的项目数。包含在第 0 节中的项目数 更新后的现有节(1)必须等于 更新前该部分中包含的项目 (1),加号或减号 从该部分插入或删除的项目数(插入 1 个, 0 已删除)加上或减去移入或移出的项目数 该部分(0 移入,0 移出)。'
这是我的代码:
// retrievedCounters is an NSMutableDictionary
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
// only want one section
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
// get the current count and add one for the "add" cell
return retrievedCounters.count + 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell;
// check whether the index path is at the very end and add the appropriate cell
if (indexPath.row == retrievedCounters.count) {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddCounter" forIndexPath:indexPath];
}
else {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CounterCell" forIndexPath:indexPath];
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
// insert new counter at index path
[self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:retrievedCounters.count-1 inSection:0]]];
}
感谢任何可以提供帮助的人!
【问题讨论】:
标签: ios objective-c uicollectionview