Вопрос

How do I reference a specific UICollectionViewCell so that each of them segue to different VCs?

Here is the code for the cells:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:self.myCellImages[0]];
    UIImage *cellImage = [[UIImage alloc] init];
    cellImage = [UIImage imageNamed:[self.myCellImages objectAtIndex:indexPath.row]];
    cell.imageView.image = cellImage;
    return cell;
}

So this displays a bunch of different cells depending on how many images I load in to my array.

say my array is:

 self.myCellImages = @[@"1.jpg", @"2.jpg",.... @"20.jpg"];

how do I reference an individual cell so that it segues into different VC? For example, clicking on the cell that has image 1.jpg segues into 1VC and clicking on the cell with17.jpg segues to 17VC

Is it something like:

if (NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];)
{
//segue here
}

I can't figure out the correct syntax

Thanks to Timothy, I added

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:indexPath
{
    NSString *storyboardId = @"main_to_VC1"; 
    [self performSegueWithIdentifier:storyboardId sender:indexPath];
}
Это было полезно?

Решение

You need to establish segues between your collection view controller (not the cell) and the destination controllers, giving each a unique storyboard ID.

Then in the collectionView:didSelectItemAtIndexPath: delegate method, decide which controller you want to segue to based on the indexPath and initiate the segue programmatically:

- (void)collectionView:collectionView didSelectItemAtIndexPath:indexPath
{
    NSString *storyboardId = ...; // determine storyboard ID based on indexPath
    [self performSegueWithIdentifier:storyboardId sender:indexPath];
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top