ios - UICollectionView didSelectRowAtIndexPath 不起作用

标签 ios objective-c uicollectionview

didSelectRowAtIndexPath 方法在我的应用中不起作用。我没有使用 NSLog 打印任何输出:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    PFObject *imageObject = [self.imageFilesArray objectAtIndex:indexPath.row];
    PFFile *imageFile = [imageObject objectForKey:@"file"];
    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (!error) {
            cell.imageView.image = [UIImage imageWithData:data];

        }
    }];

    return cell;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

        PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSLog(@"didSelectRowAtIndexPath");
    cell.imageView.image = self.image;
    return cell;
}

我希望当我点击一个单元格时,选择图像,并将图像提供给 self.image

我们可以在 segue 中实现吗?因为我的 didSelectRowAtIndexPath 方法根本不起作用。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showPhotoDetail"]) {
        NSLog(@"SEGUE showPhotoDetail");
    UICollectionViewCell *cell = sender;
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];

    PhotoDetailViewController *photoDetailViewController = (PhotoDetailViewController *)segue.destinationViewController;
        photoDetailViewController.truckImage = self.image;

    //[UIImage imageNamed:[self.imageFilesArray objectAtIndex:indexPath.row] ];
        NSLog(@"%@", photoDetailViewController.truckImage);
    }
}

最佳答案

你选择Item的方法签名是错误的,正确的方法签名是这个

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

More Info here

编辑

尝试使用此代码获取所选图像。

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    PhotoCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    self.image = cell.imageView.image;
}

编辑2

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showPhotoDetail"]) {
         // get index path of selected cell
         NSIndexPath *indexPath = [collectionView.indexPathsForSelectedItems objectAtIndex:0];
         // get the cell object
         PhotoCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
         // get image from selected cell
         self.image = cell.imageView.image;
    }
}

关于ios - UICollectionView didSelectRowAtIndexPath 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24527474/

相关文章:

ios - 如何通过单击同一个 UITableViewCell 上的另一个 UIButton 来禁用 UIButton

ios - 如何在我的应用程序中找到私有(private) iOS API 使用情况?

iphone - NSTimer 选择器调用

iphone - iOS 6 应用程序 - 如何处理 iPhone 5 屏幕尺寸?

ios - 在 UITableViewController 中启用滚动

objective-c - 获取iOS当前铃声音量值?

objective-c - 如何在 cocoa 中画一个圆圈

ios - 将目标添加到 Collection View Nib 内的按钮操作

ios - 如何向 UICollectionView 添加适当的填充?

ios - CollectionView 与单元格重叠