UICollectionView didSelectRowAtIndexPath doesn't work

匿名 (未验证) 提交于 2019-12-03 01:38:01

问题:

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

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

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

回答1:

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

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

More Info here

Edit

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

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!