【问题标题】:UICollectionViewCell is not showing DetailView when tapped点击时 UICollectionViewCell 不显示 DetailView
【发布时间】:2012-12-06 12:05:34
【问题描述】:

我在单元格中实现了一个带有 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”...

【问题讨论】:

    标签: objective-c ios uicollectionview


    【解决方案1】:

    实现collectionview委托方法

    collectionView:didSelectItemAtIndexPath:
    

    来电

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

    【讨论】:

    • 是的,解决了! :) 谢谢
    猜你喜欢
    • 1970-01-01
    • 2019-04-26
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多