在iOS中设置UICollectionView [英] Setting Up a UICollectionView in iOS

查看:79
本文介绍了在iOS中设置UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找为iOS应用设置 UICollectionView 的方法。谷歌只发布了一些博客,上面有关于 的帖子,而不是 的工作方式。当然,有Apple文档是有用的,但没有提供尽可能多的信息,因为我希望能够设置 UICollectionView

I've been hunting around for ways to setup a UICollectionView for an iOS app. Google only turns up a few blogs with posts about what it is, not how it works. Then of course, there's the Apple documentation which is helpful, but doesn't provide as much information as I'd like to be able to setup a UICollectionView.

如何设置 UICollectionView

推荐答案

uicollectionview 的课程几乎是与 uitableview 类相同。它们共享许多相同的方法和功能。如果方法/功能不同,大多数情况下只需要换掉细胞的行,反之亦然。但是,UICollectionView上有一些在UITableView上不存在的方法。首先,我将解释如何设置UICollectionView:

The uicollectionview class is almost identical to the uitableview Class. They share many of the same methods and functions. And if the methods / functions are different, most of the time it's just a matter of swapping out "row" for "cell" and vice versa. However there are a few methods that don't exist on UICollectionView that do on UITableView. First though, I'll explain how to setup a UICollectionView:


  1. 首先将UICollectionView添加到当前的ViewController,或者创建一个新的UICollectionViewController 。视图和控制器的步骤没有太大差别。

  2. 如果您使用的是View而不是ViewController,请确保委托

  3. CollectionView的strong>和 DataSource 是它所在的视图控制器。还要确保将Delegate和DataSource添加到头文件中:< UICollectionViewDataSource,UICollectionViewDelegate>
  1. Begin by adding your UICollectionView to a current ViewController, or creating a new UICollectionViewController. The steps aren't that much different for the view and controller.
  2. If you're using the View and not the ViewController, make sure that the Delegate and DataSource of the CollectionView is the view controller it's on. Also make sure to add the Delegate and DataSource to your header file: <UICollectionViewDataSource, UICollectionViewDelegate>

接下来,确保在视图控制器的类中包含这三种方法:

Next, make sure to include these three methods in your view controller's class:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath


  • 这些是唯一必需的方法。第一个告诉集合视图它应该具有的部分数量。这应该返回一个整数值。第二种方法获取每个部分中的单元格数。同样,这应该返回一个整数值。最后一个方法使用给定的数据(通常来自NSArray)填充每个单元格。最后一个方法应该返回一个CollectionViewCell。如果在此方法上设置断点,您会注意到在 numberOfItemsInSection 方法中定义的每个单元格都会调用一次断点。

  • These are the only required methods. The first tells the collection view the number of sections it should have. This should return an integer value. The second method gets the number of cells in each section. Again, this should return an integer value. The last method populates each cell using the data given (usually from an NSArray). This last method should return a CollectionViewCell. If you set breakpoints on this method, you'll notice that it is called once for every cell defined in the numberOfItemsInSection method.

    UICollectionViews提供高级动画方法,允许取消选择和选择单元格(类似于编辑模式下的页面等应用程序)。但是,据我所知,UICollectionViews不提供诸如滑动删除或类型的披露指示器等功能。

    UICollectionViews provide advanced animation methods and allow cells to be deselected and selected (similar to apps like Pages when in 'Edit' mode). However, to my knowledge, UICollectionViews do not provide features such as "swipe to delete" or and kind of disclosure indicator.

    UICollectionViews还允许您使用<创建自定义单元格a href =/ questions / tagged / xibclass =post-tagtitle =show questions tagged'xib' =tag> xib (AKA nib )文件,这允许一些非常先进的独特接口,而不需要很多复杂的代码。

    UICollectionViews also allow you to create custom cells using xib (AKA nib) files, this allows for some very advanced-looking and unique interfaces without lots of complicated code.

    可悲的是,UICollectionView仅在iOS 6及更高版本中受支持。有一些项目可供使用,例如 PSTCollectionView ,它增加了对iOS 4.3+中CollectionViews的支持,但我没有'我们想出了如何使用它们。在我的情况下,当视图加载时,我只是检查UICollectionView类是否可用,如果不是,那么我加载一个表。

    Sadly, UICollectionView is only supported in iOS 6 and up. There are a few projects available such as PSTCollectionView which adds support for CollectionViews in iOS 4.3+, but I haven't figured out how to use them. In my case, when the view loads I just check if the UICollectionView Class is available and if it isn't then I load a Table instead.

    这是一个链接到Apple关于收藏视图的官方文档。您可能还想查看此教程

    Here is a link to Apple's official documentation on Collection Views. You might also want to check out this tutorial.

    这篇关于在iOS中设置UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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