2012-12-06 2 views

답변

1

collectionview 위임 방법

collectionView:didSelectItemAtIndexPath: 

이 전화를 구현 ...

- (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"이라고하지 않을 방법 : 나는 이것을의 ViewController의 코드를

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

예, 해결했습니다. :) 고맙습니다. – rosaMerino

관련 문제