如何使用indexpath.row从UICollectionView中删除项目

时间:2013-04-30 09:19:39

标签: iphone ios uicollectionview uicollectionviewcell

我有一个集合视图,我尝试从didSelect方法的集合视图中删除一个单元格。我使用以下方法成功了

  [colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];

但现在我需要从CollectionView Cell中删除按钮上的项目。这里我只得到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删除后的单元格排列动画不存在。 请提出一个想法......提前谢谢

7 个答案:

答案 0 :(得分:56)

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

    }];
}

试试这个。它可能适合你。

答案 1 :(得分:12)

Swift 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)

答案 2 :(得分:3)

迅捷3:

 "access_token":"eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJGSjg2R2NGM2pUYk5MT2NvNE52WmtVQ0lVbWZZQ3FvcXRPUWVNZmJoTmxFIn0.eyJqdGkiOiI2YWZlZjBiMC03ZmQ1LTRiOWUtOTk3NC0yOGFjMzBkMGM5OWQiLCJleHAiOjE0OTU2MTA0NTQsIm5iZiI6MCwiaWF0IjoxNDk1NjEwMTU0LCJpc3MiOiJodHRwOi8vMTI3LjAuMC4xOjgxMDAvYXV0aC9yZWFsbXMvZXhhbXBsZSIsImF1ZCI6ImpzLWNvbnNvbGUiLCJzdWIiOiJjNGY4NjE0Zi02YjFlLTRlYjItYmYxZC0wOTJmNGYxNWQwYmIiLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiJqcy1jb25zb2xlIiwiYXV0aF90aW1lIjowLCJzZXNzaW9uX3N0YXRlIjoiNTQ4NDJjNTgtMzYxYi00MDk2LThhNjgtNGZkZTg5OGUwNzg5IiwiYWNyIjoiMSIsImNsaWVudF9zZXNzaW9uIjoiNDNjMWEzMjAtNGZmNi00NmRmLThmZjUtNTU2ZjgxNGZhYzk1IiwiYWxsb3dlZC1vcmlnaW5zIjpbXSwicmVhbG1fYWNjZXNzIjp7InJvbGVzIjpbInVzZXIiXX0sInJlc291cmNlX2FjY2VzcyI6eyJhY2NvdW50Ijp7InJvbGVzIjpbIm1hbmFnZS1hY2NvdW50IiwibWFuYWdlLWFjY291bnQtbGlua3MiLCJ2aWV3LXByb2ZpbGUiXX19LCJuYW1lIjoiU2FtcGxlIFVzZXIiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJ1c2VyIiwiZ2l2ZW5fbmFtZSI6IlNhbXBsZSIsImZhbWlseV9uYW1lIjoiVXNlciIsImVtYWlsIjoic2FtcGxlLXVzZXJAZXhhbXBsZSJ9.n1K0KIGYJYPZkjSq1eSg5gWnCAj44mgl4M-GJOlzgCj8y5TGw5OhT7sr84o9Ja9K6WMW3t0ti6feZIgc8mps3RK0HuuXeCNcrN6H2dPEtBphTvfXEUR2iMg83iCmxjhXgXso7oX2vyreJqB6WCCFEPbAQH2e5kHZqv6cfmXRlYU"

答案 3 :(得分:2)

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

试试这个。

答案 4 :(得分:1)

[array removeObjectAtIndex:[indexPath row]];

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

通常没关系......你可以看到this post,它处理相同的主题。

答案 5 :(得分:0)

我得到了答案..

在UICollectionViewCell中创建一个按钮//我将其命名为remove Btn

然后在CollectionView Delegate

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

答案 6 :(得分:0)

重要的是选择哪个IndexPath

[self.collectionView performBatchUpdates:^{
      NSIndexPath *currentIndexPath = [self.collectionView indexPathForCell:cell];
     [self.datas removeObjectAtIndex:currentIndexPath.row];
     [self.collectionView deleteItemsAtIndexPaths:@[currentIndexPath]];
} completion:nil];