2012-12-06 3 views
0

J'ai implémenté un outil CollectionView avec ImageViews dans les cellules. Je veux un DetailViewController apparaître quand la cellule est tapée. J'ai défini l'interaction utilisateur activée dans l'interface Builder pour ImageView et la cellule. Que fais-je tort? J'ai ce code dans le ViewController:UICollectionViewCell n'affiche pas DetailView lors de l'écoute

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

Mais la méthode "prepareForSegue" n'est pas appelée lorsque la cellule est prélevée ...

Répondre

1

mettre en œuvre la méthode de délégué CollectionView

collectionView:didSelectItemAtIndexPath: 

Appel

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

Oui, ça l'a corrigé! :) Thanx – rosaMerino

Questions connexes