ios 如何使用 indexpath.row 从 UICollectionView 中删除项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16296351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete an item from UICollectionView with indexpath.row
提问by LittleIDev
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
我有一个集合视图,我尝试从 didSelect 方法的集合视图中删除一个单元格。我使用以下方法成功了
[colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
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.
但是现在我需要从 CollectionView Cell.Here 中删除按钮单击时的项目。我只得到 indexpath.row。从此我无法删除该项目。我是这样试的。
-(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];
}
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
但是需要重新加载CollectionView,所以没有删除后cell排列的动画。请提出一个想法..提前致谢
回答by LittleIDev
-(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.
尝试这个。它可能对你有用。
回答by buxik
Swift 3 solution:
斯威夫特 3 解决方案:
func remove(_ i: Int) {
myObjectsList.remove(at: i)
let indexPath = IndexPath(row: i, section: 0)
self.collectionView.performBatchUpdates({
self.collectionView.deleteItems(at: [indexPath])
}) { (finished) in
self.collectionView.reloadItems(at: self.collectionView.indexPathsForVisibleItems)
}
}
and example delete call:
和示例删除调用:
self.remove(indexPath.row)
回答by Mohammad Alikhani
swift 3:
快速3:
func remove(index: Int) {
myObjectList.remove(at: index)
let indexPath = IndexPath(row: index, section: 0)
collectionView.performBatchUpdates({
self.collectionView.deleteItems(at: [indexPath])
}, completion: {
(finished: Bool) in
self.collectionView.reloadItems(at: self.collectionView.indexPathsForVisibleItems)
})
}
回答by Mohammad Alikhani
[array removeObjectAtIndex:[indexPath row]];
[collection reloadData]; // Collection is UICollectionView
Try this.
尝试这个。
回答by Plokstorm
[array removeObjectAtIndex:[indexPath row]];
[array removeObjectAtIndex:[indexPath row]];
[self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
[self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
Normally it's ok... You can see this post, it deals with same subject.
通常没问题......你可以看到这个帖子,它涉及相同的主题。
回答by Plokstorm
I got the answer..
我得到了答案..
Create a button in CollectionViewCell // I named it as removeBtn
在 CollectionViewCell 中创建一个按钮 // 我将其命名为 removeBtn
Then in CollectionView Delegate
然后在 CollectionView 委托中
- cellForItemAtIndexPath
[cell.removeBtn addTarget:self action:@selector(RemovePrssd:) forControlEvents:UIControlEventTouchUpInside];
Then add the method
然后添加方法
-(void)RemovePrssd:(id)sender{
UIView *senderButton = (UIView*) sender;
NSIndexPath *indexPath = [colleVIew indexPathForCell: (UICollectionViewCell *)[[senderButton superview]superview]];
[array removeObjectAtIndex:indexPath.row];
[colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
}