如何使用 indexpath.row 从 UICollectionView 中删除项目 [英] How to delete an item from UICollectionView with indexpath.row
问题描述
我有一个集合视图,我尝试使用 didSelect 方法从集合视图中删除一个单元格.我使用以下方法成功了
I have a collection view,and I tried to delete a cell from collection view on didSelect method.I succeeded in that using the following method
[colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
但现在我需要从 CollectionView Cell 中删除按钮单击时的项目.这里我只得到 indexpath.row.从此我无法删除该项目.我试过这样.
But now I need to delete the item on button click from CollectionView Cell.Here I get only the indexpath.row. From this I am not able to delete the Item. I tried like this.
-(void)remove:(int)i {
NSLog(@"index path%d",i);
[array removeObjectAtIndex:i];
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:i inSection:0];
[colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
[colleVIew reloadData];
}
但它需要重新加载CollectionView.所以删除后的单元格排列动画不存在.请提出一个想法..提前谢谢
But it needs to reload the CollectionView.So the animation of cell arrangement after deletion is not there. Please suggest an idea..thanks in advance
推荐答案
-(void)remove:(int)i {
[self.collectionObj performBatchUpdates:^{
[array removeObjectAtIndex:i];
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:i inSection:0];
[self.collectionObj deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
} completion:^(BOOL finished) {
}];
}
试试这个.它可能对你有用.
Try this. It may work for you.
这篇关于如何使用 indexpath.row 从 UICollectionView 中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!