UICollectionViewCell在点击时不显示DetailView
问题描述:
我在单元格中用ImageViews实现了一个CollectionView。我想要一个DetailViewController在单元被点击时出现。我在Interface Builder中为ImageView和Cell设置了User Interaction Enabled。我在做什么错了?我有这样的代码中的ViewController:UICollectionViewCell在点击时不显示DetailView
- (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”的方法...
答
实现的CollectionView委托方法
collectionView:didSelectItemAtIndexPath:
那里调用
[self performSegueWithIdentifier:@"showDetail" sender:self];
是的,修好了! :) Thanx – rosaMerino