UICollectionView didSelectRowAtIndexPath 不起作用 [英] UICollectionView didSelectRowAtIndexPath doesn't work

查看:27
本文介绍了UICollectionView didSelectRowAtIndexPath 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The didSelectRowAtIndexPath method doesn't work in my app. I don't get printed any output with 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.

I would like that when I tap on a cell, that take the image selected, and give the image to self.image.

我们可以在segue中做到这一点吗?因为我的 didSelectRowAtIndexPath 方法根本不起作用.

Can we make that in segue? Because my didSelectRowAtIndexPath method doesn't work at all.

-(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的方法签名有误,正确的方法签名是这个

Your method signature of selecting Item is wrong, the correct method signature is this

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

更多信息在这里

试试这个代码来获取选定的图像.

try this code to get the selected image.

- (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;
    }
}

这篇关于UICollectionView didSelectRowAtIndexPath 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆