4

我有一组图像显示在UICollectionView. 当用户点击图像时,它会生成UIActionSheet带有该图像的一些选项的 a。其中一个 ID 从UICollectionView. 当用户在 中选择删除按钮时UIActionSheet,它会弹出一个警报视图,要求确认。如果用户选择是,它应该删除照片。

我的问题是,要从 中删除项目UICollectionView,您必须将 传递indexPathdeleteItemsAtIndexPaths事件。由于最终确认是在警报视图的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
{

}
4

1 回答 1

9

为什么不使用[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]];
}
于 2013-04-18T10:29:47.270 回答