2012-12-06 50 views
0

我在單元格中用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」的方法...

回答

1

實現的CollectionView委託方法

collectionView:didSelectItemAtIndexPath: 

那裏調用

[self performSegueWithIdentifier:@"showDetail" sender:self]; 
+0

是的,修好了! :) Thanx – rosaMerino