4

I have a set of images showing in a UICollectionView. When the user taps on an image, it spawns a UIActionSheet with a few options for that image. One of them id removing the photo from the UICollectionView. When the user selects remove button in the UIActionSheet, it pops up an alert view asking for confirmation. If the user selects yes, it should remove the photo.

My problem is, to remove the item from the UICollectionView, you have to pass the indexPath to the deleteItemsAtIndexPaths event. Since the final confirmation is granted in the alert view's didDismissWithButtonIndex event, I can't figure out a way to get the indexPath of the selected image from there to pass it to deleteItemsAtIndexPaths event. How can I do this?

Here's my code:

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

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Isuru
  • 30,617
  • 60
  • 187
  • 303

1 Answers1

9

Why not make use of [self.collectionView indexPathsForSelectedItems]; . I have done this for deleting multiple images at a time.

- (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
}

Edit

-(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]];
}
idmean
  • 14,540
  • 9
  • 54
  • 83
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
  • I'm getting a **No visible @interface for 'SomeViewController' declares the selected 'deleteItemsFromDataSourceAtIndexPaths'** error at the line, `[self deleteItemsFromDataSourceAtIndexPaths:selectedItemsIndexPaths];`. – Isuru Apr 18 '13 at 10:48
  • Sory i forgot to make a point __deleteItemsFromDataSourceAtIndexPaths__ is my custom function to delete the selected images from my data source array. You can directly do the operation there itself. Anwy its important to delete images from your datasource array before calling __deleteItemsAtIndexPaths:__ – Anil Varghese Apr 18 '13 at 10:52
  • I see. Now I'm trying to get remove the image from the data source array using `removeObjectAtIndex`. But since it expects an integer value, how can I get that from the `selectedItemsIndexPaths` array? – Isuru Apr 18 '13 at 11:34
  • Hello, Is there a way to get a single Index out of the `NSMutableIndexSet`? – Isuru Apr 19 '13 at 04:30
  • I didnt get you. Why you need that. I want to delete multiple items so that i go for index set. Yo can do some thing like [self.images removeObjectAtIndex [selectedItemsIndexPaths objectAtIndex:1 ].row]]. Got?? – Anil Varghese Apr 19 '13 at 04:51
  • Oh sorry about that. Deleting is fine. Now what I'm trying to do now is when the user taps on a small image in the `UICollectionView`, it should segue to another `UIViewController` with a `UIImageView` so that the image can be displayed in full size. For that I need to get the single index of that particular image from the data source. Please see the code snippet [here](http://pastebin.com/CNfujJKq) – Isuru Apr 19 '13 at 05:07
  • I didnt get your code since i have some network restrictions in my company. See my edit of answer may help u – Anil Varghese Apr 19 '13 at 05:45
  • So it seems that my images needs to be an NSMutableArray for removeObjectsAtIndexes property to appear. my "images" array is [NSManagedObject] because it comes from Core Data. Do I really need to look through and covert it into NSMutableArray? – Ace Green Jun 06 '15 at 14:21