Dave
Dave

Reputation: 1950

UICollectionView custom cell, how to select prior to segue when tapping a UIButton

I have a UICollectionView embedded in a UIViewController. I have a custom UICollectionView cell with a button in the CollectionView. I'd like to tap the button and have it push a detail view controller and pass a little data while it's at it.

I can get a hold of which cell fairly easily via prepareForSegue in my master view controller

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
    NSIndexPath *index = [indexPaths objectAtIndex:0];
    DetailController *destViewController = segue.destinationViewController;
    }

Which would work if I tapped on the cell itself. However, I have a UIButton i want tapped and I need to find a way to hook into indexPathsForSelectedItems ... Right now, my button won't work properly unless I actually tap the cell that it's in first. Do I need to programatically select the cell it's in before firing the tap event?

how would I do this? WHERE would I do this? On the viewcontroller? or within a tap event in my custom UICollectionViewCell?

Upvotes: 1

Views: 2050

Answers (1)

AlexanderZ
AlexanderZ

Reputation: 2158

Please check out the link http://ashfurrow.com/blog/uicollectionview-example.

It's not a simple example but it has the answer. And there is a link to github so you may play with it.

Upvotes: 0

Related Questions