0

我在单元格中实现了一个带有 ImageViews 的 CollectionView。我希望在点击单元格时出现 DetailViewController。我在 ImageView 和 Cell 的 Interface Builder 中设置了 User Interaction Enabled。我做错了什么?我在 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;
    }
}

但是当点击单元格时,不会调用“prepareForSegue”方法......

4

1 回答 1

1

实现collectionview委托方法

collectionView:didSelectItemAtIndexPath:

有电话

[self performSegueWithIdentifier:@"showDetail" sender:self];
于 2012-12-06T12:20:26.880 回答