[英]How to delete an item from UICollectionView with indexpath.row
我有一個集合視圖,我嘗試從 didSelect 方法的集合視圖中刪除一個單元格。我使用以下方法成功了
[colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
但是現在我需要從 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];
}
但是需要重新加載CollectionView,所以沒有刪除后cell排列的動畫。 請提出一個想法..提前致謝
-(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) {
}];
}
試試這個。 它可能對你有用。
斯威夫特 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)
}
}
和示例刪除調用:
self.remove(indexPath.row)
快速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)
})
}
[array removeObjectAtIndex:[indexPath row]];
[collection reloadData]; // Collection is UICollectionView
試試這個。
[array removeObjectAtIndex:[indexPath row]];
[self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
通常沒問題......你可以看到這篇文章,它涉及相同的主題。
我得到了答案..
在 CollectionViewCell 中創建一個按鈕 // 我將其命名為 removeBtn
然后在 CollectionView 委托中
- cellForItemAtIndexPath
[cell.removeBtn addTarget:self action:@selector(RemovePrssd:) forControlEvents:UIControlEventTouchUpInside];
然后添加方法
-(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]];
}
重要的是您選擇了哪個IndexPath
。
[self.collectionView performBatchUpdates:^{
NSIndexPath *currentIndexPath = [self.collectionView indexPathForCell:cell];
[self.datas removeObjectAtIndex:currentIndexPath.row];
[self.collectionView deleteItemsAtIndexPaths:@[currentIndexPath]];
} completion:nil];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.