滚动视图还是集合视图? [英] ScrollView or CollectionView?

查看:23
本文介绍了滚动视图还是集合视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个填充数据的集合视图,例如:

I have a collection view populated with data like:

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

然后转到静态的详细信息视图.

and then have a segue to a detail view which is static.

我现在想在详细视图中添加分页,这样我就可以像 iPhone 照片库的工作方式一样滑动我收藏视图中包含的图像.

I now want to add paging to the detail view so that I can swipe through the images that I have contained in my collection view in the same way that the iPhone photo gallery works.

我猜需要一个滚动视图或第二个集合视图,但是我不确定从哪里开始,因此非常感谢你们提供的任何指针、示例代码或任何其他帮助.

I'm guessing either a scrollview or a second collectionview would be needed however I'm not sure where to start with this so any pointers, sample code or any other help you guys could give would be massively appreciated.

谢谢

推荐答案

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

If you already have a collection view that does what you want just turn on paging. self.collectionView.pagingEnabled = YES You can also enable paging in IB by checking the paging box in the attributes inspector. Using a UICollectionView gives you the added benefit of reusable cells.

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

You could still use UICollectionView. Create an UICollectionViewCell subclass that contains your detail view. Then when you want to launch directly to a particular image you can use - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated If you're loading a ton of images in this view the scrollview method will load all of your images at once rather than on demand like they would in a collectionview scenario. You can always work around that in a scrollview but all that work's been done for you in UICollectionView.

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

I started this in a comment but it was getting big...

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

In your view controller subclass you will need to register either your class or a nib that you created to lay out that cell. I prefer the nib so I can lay everything out in Interface Builder. If you go the nib route you will create an empty xib and drag a UICollectionViewCell out of the object library in the bottom right. Then select that cell and make sure the Class is set to the custom subclass you've created.

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

Now in your view controller subclass you'll call – registerNib:forCellWithReuseIdentifier: in the viewDidLoad method like this:

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

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

Your view controller will conform to the UICollectionViewDataSource protocol and you will implement this method:

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

这篇关于滚动视图还是集合视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆