ScrollView还是CollectionView?

我有一个集合视图,其中包含以下数据:

[self.graniteImages addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Angel Cream", @"name", @"angel-cream.jpg", @"image", nil]]; [self.graniteImages addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Angola Black", @"name" , @"angola_black1.jpg", @"image" , nil]]; 

然后有一个segue到一个静态的详细视图。

我现在想要在细节视图中添加分页,这样我就可以像我的iPhone照片库一样轻扫我收集在视图中的图像。

我猜想要么是滚动视图还是第二次收集视图,但我不知道从哪里开始,所以任何指针,示例代码或任何其他帮助你们都可以给予大力赞赏。

谢谢

如果您已经有一个集合视图,可以执行您想要的操作,只需打开分页即可。 self.collectionView.pagingEnabled = YES您还可以通过检查属性检查器中的分页框来启用IB中的分页。 使用UICollectionView为您提供可重用单元的额外好处。

编辑

您仍然可以使用UICollectionView。 创建一个包含详细信息视图的UICollectionViewCell子类。 然后,当你想直接启动特定图像时,你可以使用- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated如果你在这个视图中加载了大量的图像scrollview方法将一次加载所有图像,而不是像在集合视图场景中那样按需加载。 您总是可以在滚动视图中解决这个问题,但所有工作都是在UICollectionView中完成的。

EDIT2

我在评论中开始这个,但它变得越来越大……

在视图控制器子类中,您需要注册您创建的类或nib以布置该单元格。 我更喜欢笔尖,所以我可以在Interface Builder中放置所有内容。 如果你去了nib路径,你将创建一个空的xib并将UICollectionViewCell拖出右下角的对象库。 然后选择该单元格并确保将Class设置为您创建的自定义子类。

现在在你的视图控制器子类中,你将调用– registerNib:forCellWithReuseIdentifier:在viewDidLoad方法中,如下所示:

  [self.collectionView registerNib:[UINib nibWithNibName:@"WhateverYouNamedYourNib" bundle:nil] forCellWithReuseIdentifier:@"cell"]; 

您的视图控制器将符合UICollectionViewDataSource协议,您将实现此方法:

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CustomCellClass *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath //Setup the cell with image and whatever else return cell; } 

您应该只使用启用了分页的UIScrollView。 以下代码应该可以解决问题:

 NSInteger numberOfImages = [self.graniteImages count]; CGRect bounds = self.scrollView.bounds; self.scrollView.contentSize = CGSizeMake(bounds.size.width * numberOfImages, bounds.size.height); scrollView.pagingEnabled = YES; CGFloat x = 0; for (NSMutableDictionary *dict in images) { UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[dict valueForKey:@"image"]]]; imageView.frame = CGRectMake(x, 0, bounds.size.width, bounds.size.height); [scrollView addSubview:imageView]; x += self.view.bounds.size.width; } 

编辑:此代码将在您的详细信息视图控制器的ViewDidLoad方法中。 您的详细信息视图控制器还应具有NSUInteger类型的属性索引和NSMutableArray *类型的graniteImages属性,您可以在集合视图控制器的prepareForSegue方法中设置,如下所示:

 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if( [segue.identifier isEqualToString:@"yourSegue"]){ ((DetailViewController*)segue.destinationViewController).graniteImages = self.graniteImages; ((DetailViewController*)segue.destinationViewController).index = self.tappedImageIndex; } } 

并在您的ViewDidLoad方法中添加:

 [scrollView scrollRectToVisible:CGRectMake(bounds.size.width * self.index, 0, bounds.size.width, bounds.size.height) animated:NO];