ScrollView还是CollectionView?

时间:2013-05-22 14:14:50

标签: ios uiscrollview uicollectionview

我有一个用以下数据填充的集合视图:

[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照片库中一样,扫描我在集合视图中包含的图像。

我猜测需要滚动视图或第二个收集视图,但是我不知道从哪里开始,所以任何指针,示例代码或任何其他帮助你们都会非常感激。

由于

2 个答案:

答案 0 :(得分:5)

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

修改

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

EDIT2

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

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

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

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

答案 1 :(得分:0)

您应该只使用启用了分页的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];