2013-03-29 41 views
1

我正在按照教程进行操作并溢出UICollectionView。此外,我想有一个项目的选择,然后出现一个新的UIViewControllerUICollectionView的选择

请问我该怎么做? 我可以像UITable一样选择选择索引= 0,1或2等吗? 代码显示正确NSLog但不会去其他UIViewController

- (void)tapCell:(UITapGestureRecognizer *)inGestureRecognizer 
{ 
    NSIndexPath *theIndexPath = [self.collectionView indexPathForCell:(UICollectionViewCell *)inGestureRecognizer.view]; 

    if (theIndexPath.row ==0) { 
     NSLog(@"****"); 
     page *noteDetailViewControler = [[page alloc] initWithNibName:@"page" bundle:nil]; } 
} 

下面是完整的代码:

@implementation CDemoCollectionViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.cellCount = 10; 
    self.imageCache = [[NSCache alloc] init]; 

    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CCoverflowTitleView class]) bundle:NULL] forSupplementaryViewOfKind:@"title" withReuseIdentifier:@"title"]; 

    NSMutableArray *theAssets = [NSMutableArray array]; 
    NSURL *theURL = [[[NSBundle mainBundle] resourceURL] URLByAppendingPathComponent:@"Images"]; 
    NSEnumerator *theEnumerator = [[NSFileManager defaultManager] enumeratorAtURL:theURL includingPropertiesForKeys:NULL options:NSDirectoryEnumerationSkipsPackageDescendants | NSDirectoryEnumerationSkipsHiddenFiles errorHandler:NULL]; 
    for (theURL in theEnumerator) 
    { 
     if ([[theURL pathExtension] isEqualToString:@"jpg"]) 
     { 
      [theAssets addObject:theURL]; 
     } 
    } 
    self.assets = theAssets; 
    self.cellCount = self.assets.count; 
} 

#pragma mark - 

- (void)updateTitle 
{ 
    // Asking a collection view for indexPathForItem inside a scrollViewDidScroll: callback seems unreliable. 
    // NSIndexPath *theIndexPath = [self.collectionView indexPathForItemAtPoint:(CGPoint){ CGRectGetMidX(self.collectionView.frame) + self.collectionView.contentOffset.x, CGRectGetMidY(self.collectionView.frame) }]; 
    NSIndexPath *theIndexPath = ((CCoverflowCollectionViewLayout *)self.collectionView.collectionViewLayout).currentIndexPath; 
    if (theIndexPath == NULL) 
    { 
     self.titleView.titleLabel.text = NULL; 
    } 
    else 
    { 
     NSURL *theURL = [self.assets objectAtIndex:theIndexPath.row]; 

     self.titleView.titleLabel.text = [NSString stringWithFormat:@"%@", theURL.lastPathComponent]; 
    } 
} 

#pragma mark - 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section; 
{ 
    return(self.cellCount); 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    CDemoCollectionViewCell *theCell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"DEMO_CELL" forIndexPath:indexPath]; 

    if (theCell.gestureRecognizers.count == 0) 
    { 
     [theCell addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCell:)]]; 
    } 

    theCell.backgroundColor = [UIColor colorWithHue:(float)indexPath.row/(float)self.cellCount saturation:0.333 brightness:1.0 alpha:1.0]; 

    if (indexPath.row < self.assets.count) 
    { 
     NSURL *theURL = [self.assets objectAtIndex:indexPath.row]; 
     UIImage *theImage = [self.imageCache objectForKey:theURL]; 
     if (theImage == NULL) 
     { 
      theImage = [UIImage imageWithContentsOfFile:theURL.path]; 

      [self.imageCache setObject:theImage forKey:theURL]; 
     } 

     theCell.imageView.image = theImage; 
     theCell.reflectionImageView.image = theImage; 
     theCell.backgroundColor = [UIColor clearColor]; 
    } 

    return(theCell); 
} 

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 
{ 
    CCoverflowTitleView *theView = [self.collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"title" forIndexPath:indexPath]; 
    self.titleView = theView; 
    [self updateTitle]; 
    return(theView); 
} 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    [self updateTitle]; 
} 

#pragma mark - 

- (void)tapCell:(UITapGestureRecognizer *)inGestureRecognizer 
{ 
    NSIndexPath *theIndexPath = [self.collectionView indexPathForCell:(UICollectionViewCell *)inGestureRecognizer.view]; 

    if (theIndexPath.row ==0) { 
     NSLog(@"****"); 
     page *noteDetailViewControler = [[page alloc] initWithNibName:@"page" bundle:nil]; } 
} 


@end 
+0

http://stackoverflow.com/questions/15091847/i-have-a-uicollectionview-and-i-want-to-show-an-image-in-a-cell-that-goes-to- A/15105450#15105450 – Alex

回答

5

尝试使用以下UICollectionViewDelegate功能:

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

如果你得到选择后,可以使用以下功能来处理它:

- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition; 
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated; 
相关问题