【发布时间】:2015-04-17 15:16:11
【问题描述】:
但是,我正在尝试从我的 collectionView 中删除一个单元格。当我从数据源中删除对象并尝试批量更新时,它说单元格不再存在。:
'NSInternalInconsistencyException', reason: 'attempt to delete item 0 from section 0 which only contains 0 items before the update'
在此代码之前,我从核心数据中删除内容,[[usermanager getSelectedUser]loadCards]; 行实际上通过从核心数据中获取单元格内容来重新加载包含单元格内容的数据源。
- (void)cardRemoved:(NSNotification *)note {
NSDictionary *args = [note userInfo];
Card *card = [args objectForKey:@"card"];
[[usermanager getSelectedUser]loadCards];
[self.collectionView performBatchUpdates:^{
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:card.position.intValue inSection:0];
[self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
} completion:^(BOOL finished){
[self.collectionView setDelegate:self];
[self.collectionView setDataSource:self];
[self.collectionView reloadData];
}];
}
如果我在调用loadCards 行之前打印出单元格的数量,我会得到正确的行数(如预期的那样)。
编辑 这就是 loadCards 所说的:
-(NSMutableArray *)getCards{
UserModel *selectedUser = [self getSelectedUserFromDB];
NSMutableArray *cards = [[NSMutableArray alloc] init];
for(CardModel *cardModel in selectedUser.cards){
[cards addObject:[self modelToCard:cardModel]];
}
return cards;
}
我注意到,即使我不调用loadCards 方法,它也会说视图中没有项目。
谁能帮帮我?谢谢
【问题讨论】:
-
你能发布 loadCards 的定义吗?我猜你是在尝试过早删除一个单元格,如果它之前都加载了该行
-
@KevinDiTraglia 添加了更多信息
标签: ios objective-c uicollectionview