I’m doing something that involves a collection view with photos in it, and upon selecting one of the cells, it will segue into a new view where it shows a larger image.
Heres prepare for segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"showPhotoSegue"]) {
NSIndexPath *ip = [self.photoCollectionView indexPathForCell:sender];
PhotoDisplayViewController *viewController = segue.destinationViewController;
Photo* photo = [self.fetchedResultsController objectAtIndexPath:ip];
NSLog(@"setting PHOTO at indexPath %@", ip);
[viewController setPhoto:[UIImage imageWithContentsOfFile:photo.url]];
}
}
And heres didSelectItemAtIndexPath
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier = @"showPhotoSegue";
[self performSegueWithIdentifier:identifier sender:self];
NSLog(@"Selected item at %@", indexPath);
}
My view is always empty so i added the print line statements, and it seems the output is always something like
Selected cell at <NSIndexPath 0x1e084940> 2 indexes [0, 0], detail view controller
So my question is, why is NSIndexPath always a pair of 2 indicies, and also how do i use it in my prepareforsegue to set the view for the segue
Thanks
In
prepareForSegue:sender:, you expectsenderto be aUICollectionViewCell.In
collectionView:didSelectItemAtIndexPath:, you are passingself(yourUICollectionViewDelegate) as thesenderargument. I suspect your collection view delegate is not a collection view cell.Instead of sending the collection view delegate as
sender, and expecting to receive a cell assender, why not pass the index path assenderand expect to receive it assender?