简体   繁体   中英

UICollectionViewCell is not showing DetailView when tapped

I implemented a CollectionView with ImageViews in the cells. I want a DetailViewController to appear when the cell is tapped. I set the User Interaction Enabled in the Interface Builder for the ImageView and the Cell. What am I making wrong? I have this code in the ViewController:

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
    return 9;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{

    PhotosCell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];



    // load the image for this cell
    NSString *imageToLoad = [NSString stringWithFormat:@"%d.jpg", indexPath.row +1];
    // NSLog(imageToLoad);
    cell.imageView.image = [UIImage imageNamed:imageToLoad];

    return cell;
}

// the user tapped a collection item, load and set the image on the detail view controller
//
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   // NSLog(@"inside!");
    if ([[segue identifier] isEqualToString:@"showDetail"])
    {
        NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];

        // load the image, to prevent it from being cached we use 'initWithContentsOfFile'
        NSString *imageNameToLoad = [NSString stringWithFormat:@"%d_full", selectedIndexPath.row+1];
        NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@".jpg"];
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];

        PhotoDetailViewController *detailViewController = [segue destinationViewController];
        detailViewController.myImage = image;
    }
}

But the method "prepareForSegue" is not being called when the cell is tapped...

implement collectionview delegate method

collectionView:didSelectItemAtIndexPath:

there call

[self performSegueWithIdentifier:@"showDetail" sender:self];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM