从UICollectionView中删除一个项目

我有一组图像显示在UICollectionView 。 当用户点击图像时,会产生一个UIActionSheet其中包含该图像的几个选项。 其中一个ID从UICollectionView删除照片。 当用户在UIActionSheetselect删除button时,它会popup一个警告视图,要求确认。 如果用户select是,它应该删除照片。

我的问题是,要从UICollectionView删除项目,您必须将indexPath传递给deleteItemsAtIndexPaths事件。 由于最终确认是在alert视图的didDismissWithButtonIndex事件中授予的,所以我找不到从这里获取所选图像的indexPath并将其传递给deleteItemsAtIndexPaths事件的方法。 我怎样才能做到这一点?

这是我的代码:

 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { switch (buttonIndex) { case 0: deletePhotoConfirmAlert = [[UIAlertView alloc] initWithTitle:@"Remove Photo" message:@"Do you want to remove this photo?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil]; [deletePhotoConfirmAlert addButtonWithTitle:@"Yes"]; [deletePhotoConfirmAlert show]; break; case 1: NSLog(@"To Edit photo"); break; } } - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { if (alertView == deletePhotoConfirmAlert) { if (buttonIndex == 1) { // Permission to delete the button is granted here. // From here deleteItemsAtIndexPaths event should be called with the indexPath } } } - (void)deleteItemsAtIndexPaths:(NSArray *)indexPaths { } 

为什么不使用[self.collectionView indexPathsForSelectedItems]; 。 我一次删除了多个图像。

 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { if (alertView == deletePhotoConfirmAlert) { if (buttonIndex == 1) { // Permission to delete the button is granted here. NSArray *selectedItemsIndexPaths = [self.collectionView indexPathsForSelectedItems]; // Delete the items from the data source. [self deleteItemsFromDataSourceAtIndexPaths:selectedItemsIndexPaths]; // Now delete the items from the collection view. [self.collectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths]; } } } // This method is for deleting the selected images from the data source array -(void)deleteItemsFromDataSourceAtIndexPaths:(NSArray *)itemPaths { NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet]; for (NSIndexPath *itemPath in itemPaths) { [indexSet addIndex:itemPath.row]; } [self.images removeObjectsAtIndexes:indexSet]; // self.images is my data source } 

编辑

 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSArray *indexpaths = [self.collectionView indexPathsForSelectedItems]; DetailViewController *dest = [segue destinationViewController]; dest.imageName = [self.images objectAtIndex:[[indexpaths objectAtIndex:0] row]]; }