点击时,UICollectionViewCell不显示DetailView

时间:2012-12-06 12:05:35

标签: objective-c ios uicollectionview

我在单元格中使用ImageViews实现了CollectionView。我希望在点击单元格时出现DetailViewController。我在Interface Builder中为ImageView和Cell设置了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”...

1 个答案:

答案 0 :(得分:1)

实现collectionview委托方法

collectionView:didSelectItemAtIndexPath:

有电话

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