ios – UICollectionView didSelectRowAtIndexPath不起作用

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不起作用全部内容,希望文章能够帮你解决ios – UICollectionView didSelectRowAtIndexPath不起作用所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。