Indexpath of the button from UICollectionView
OK, here it is:
- (IBAction)delete:(UIButton *)sender{
NSIndexPath *indexPath = nil;
indexPath = [self.collectionView indexPathForItemAtPoint:[self.collectionView convertPoint:sender.center fromView:sender.superview]];
}
Sometimes it's the simplest possible answer. I was having exactly the same problem as @Schmidt, but was frustrated to see that his answer was to use indexPathForItemAtPoint:, as though indexPathForCell: was somehow broken and couldn't be used as intended.
Then I tried his solution, and still had the same result: the index path was coming back nil.
Solution: the view controller's collectionView outlet wasn't connected to the UICollectionView instance in the NIB (on the storyboard). After making that missing connection, both methods (indexPathForCell: and indexPathForItemAtPoint) worked as expected.
I know other devs run into this problem sometimes, so take this as a reminder: the problem may not be your code, per se, but rather something in Interface Builder like an unconnected outlet (or just as confusing, an outlet that's connected to something which no longer exists).