从UICollectionView中删除项目

时间:2013-04-18 10:20:39

标签: ios uicollectionview uialertview uiactionsheet

我在UICollectionView中显示了一组图片。当用户点击图像时,它会生成UIActionSheet,其中包含该图像的几个选项。其中一人正在从UICollectionView删除照片。当用户在UIActionSheet中选择删除按钮时,会弹出一个要求确认的警报视图。如果用户选择“是”,则应删除照片。

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

}

1 个答案:

答案 0 :(得分: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]];
}