繁体   English   中英

如何从动画中删除UICollectionView中唯一的单元格(deleteItemsAtIndexPaths)?

[英]How to delete the only cell from UICollectionView with animation (deleteItemsAtIndexPaths)?

我从UICollectionView删除单个UICollecitonViewCell时得到一个断言。

前提条件:我有一个单元格(当我有两个或更多单元格时,删除效果很好)。

这是代码:

    NSIndexPath *ip = [_photosCollectionView indexPathForCell:cell];

    [_datasource removeItemAtIndex:ip.item];

    [_photosCollectionView deleteItemsAtIndexPaths:@[ip]]; // the assertion is raised

这是断言文本:

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

非常奇怪的问题,因为它适用于2,3或更多单元格,但是当我删除单个单元格时,它会失败。

任何想法有什么不对,如何解决这个问题?

感谢SO上的类似问题和答案,找到了使用performBatchUpdates的解决方案:

[_photosCollectionView performBatchUpdates:^ {
    [_datasource removeItemAtIndex:ip.item];
    [_photosCollectionView deleteItemsAtIndexPaths:@[ip]]; // no assertion now
} completion:nil];

与Swift 4.1相同的解决方案

    let indexPath = IndexPath(item: 0, section: 0)
    self.collectionView.performBatchUpdates({            
        self.collectionView.deleteItems(at:[indexPath])
    }, completion:nil)

暂无
暂无

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

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