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

查看:19
本文介绍了在 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:

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,请确保 CollectionView 的 DelegateDataSource 是它所在的视图控制器.还要确保将 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 提供高级动画方法并允许取消选择和选择单元格(类似于处于编辑"模式下的 Pages 等应用程序).然而,据我所知,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 还允许您使用 (AKA ) 文件,这允许一些非常高级且独特的界面,无需大量复杂的代码.

    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.

    这里是苹果官方文档的链接 收藏视图.您可能还想查看此教程.

    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天全站免登陆