简体   繁体   English

UICollectionView:消除单元格问题

[英]UICollectionView: removing cell issue

I am trying to remove a cell from my collectionView, however. 我试图从我的collectionView中删除一个单元格。 When I remove the object from my datasource and attempt a batchupdate, it says the cell doesn't exist anymore.: 当我从数据源中删除该对象并尝试进行批处理更新时,它说该单元不再存在。

'NSInternalInconsistencyException', reason: 'attempt to delete item 0 from section 0 which only contains 0 items before the update'

Before this code I remove the content from my core data, and the line [[usermanager getSelectedUser]loadCards]; 在此代码之前,我从核心数据和[[usermanager getSelectedUser]loadCards];行中删除了内容[[usermanager getSelectedUser]loadCards]; actually reloads the datasource containing the content for the cells by getting them from the Core Data. 实际上是通过从核心数据获取单元格来重新加载包含单元格内容的数据源。

- (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];

    }];
}

If I print out the amount of Cells before I call the loadCards line, I get the correct amount of rows(As expected). 如果在调用loadCards行之前打印出单元格的数量,我将得到正确的行数(如预期的那样)。

EDIT This is what loadCards calls: 编辑这是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;
}

I noticed, even if I don't call the loadCards method, It says there are no items in the view. 我注意到,即使不调用loadCards方法,它也表示视图中没有项目。

Can anyone help me out? 谁能帮我吗? Thank you 谢谢

Remove the cells from the UICollectionView , then remove them from the model. UICollectionView删除单元格, 然后从模型中删除它们。 The same thing applies to UITableView . 同样的情况适用于UITableView You also don't need to reload the collection view after removing items. 您也无需在删除项目后重新加载收藏视图。

If you prefer you can just remove items from the model and then reload the collection view and the items that aren't in the model will disappear from the collection view, but without the same animation that comes from removing items from a collection view. 如果您愿意,您可以只从模型中删除项目,然后重新加载集合视图,不在模型中的项目将从集合视图中消失,但是不会具有从集合视图中删除项目相同的动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM