在 iOS 中设置 UICollectionView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12654281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:31:09  来源:igfitidea点击:

Setting Up a UICollectionView in iOS

objective-ciosuicollectionview

提问by Sam Spencer

I've been hunting around for ways to setup a UICollectionViewfor an iOS app. Google only turns up a few blogs with posts about whatit is, not howit 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为 iOS 应用程序设置一个的方法。谷歌只出现了一些关于它是什么的帖子,而不是它是如何工作的。当然,还有 Apple 文档很有帮助,但没有提供我希望能够设置UICollectionView.

How can one setup a UICollectionView?

如何设置一个UICollectionView

回答by Sam Spencer

The uicollectionviewclass is almost identical to the uitableviewClass. 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:

所述uicollectionview类是几乎相同的UITableView的类。它们共享许多相同的方法和功能。如果方法/功能不同,大多数情况下只是将“行”换成“单元格”,反之亦然。然而,有一些方法在 UICollectionView 上不存在,但在 UITableView 上是这样的。不过,首先,我将解释如何设置 UICollectionView:

  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 Delegateand DataSourceof the CollectionView is the view controller it's on. Also make sure to add the Delegate and DataSource to your header file: <UICollectionViewDataSource, UICollectionViewDelegate>

  3. 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
    
  4. 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 numberOfItemsInSectionmethod.

  1. 首先将您的 UICollectionView 添加到当前的 ViewController,或创建一个新的 UICollectionViewController。视图和控制器的步骤没有太大不同。
  2. 如果您使用的是 View 而不是 ViewController,请确保CollectionView的DelegateDataSource是它所在的视图控制器。还要确保将 Delegate 和 DataSource 添加到您的头文件中:<UICollectionViewDataSource, UICollectionViewDelegate>

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

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

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 提供高级动画方法并允许取消选择和选择单元格(类似于处于“编辑”模式时的 Pages 等应用程序)。但是,据我所知,UICollectionViews 不提供诸如“滑动删除”或披露指示符等功能。

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.

UICollectionViews 还允许您使用xib(AKA nib) 文件创建自定义单元格,这允许一些非常高级且独特的界面,而无需大量复杂的代码。

Sadly, UICollectionView is only supported in iOS 6 and up. There are a few projects available such as PSTCollectionViewwhich 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.

遗憾的是,UICollectionView 仅在 iOS 6 及更高版本中受支持。有一些可用的项目,例如PSTCollectionView,它在 iOS 4.3+中添加了对 CollectionViews 的支持,但我还没有弄清楚如何使用它们。就我而言,当视图加载时,我只检查 UICollectionView 类是否可用,如果不可用,则我加载一个表。

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

这是 Apple 关于Collection Views的官方文档的链接。您可能还想查看本教程

回答by bryguy1300

I created a step-by-step tutorialfor setting up UICollectionViews with custom layouts. Hopefully it helps some people to get familiar with the API.

我创建了一个分步教程,用于使用自定义布局设置 UICollectionViews。希望它可以帮助一些人熟悉 API。