如何从UICollectionView删除项目与indexpath.row(How to delet

2019-09-01 11:20发布

我有一个集合看法,我试图删除集合视图细胞上didSelect method.I成功,使用下面的方法

  [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.So细胞排列的动画后删除不存在。 请建议提前一个idea..thanks

Answer 1:

-(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) {

    }];
}

试试这个。 它可能为你工作。



Answer 2:

斯威夫特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)


Answer 3:

SWIFT 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)
    })
}


Answer 4:

[array removeObjectAtIndex:[indexPath row]];
    [collection reloadData]; // Collection is UICollectionView

试试这个。



Answer 5:

[array removeObjectAtIndex:[indexPath row]];

[self.collectionView deleteItemsAtIndexPaths:@[indexPath]];

通常它是确定......你可以看到这篇文章 ,它与相同主题的交易。



Answer 6:

我得到了答案..

创建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]];
  }


文章来源: How to delete an item from UICollectionView with indexpath.row