iOS UICollectionView示例教程

时间:2020-02-23 14:45:55  来源:igfitidea点击:

在本教程中,我们将研究iOS UICollectionView组件并从中开发一个基本应用程序。

UICollectionView

UICollectionView与UITableView类似,不同之处在于它更具可定制性。
UICollectionView和UITableView之间的主要区别在于,UICollectionView可以显示多个列,并且支持水平滚动。

UICollectionView用于以我们希望的任何方式自定义布局。
基本的UICollectionView在很多方面都类似于android的GridView。

在"照片"应用中可以看到UICollectionView布局的最常见位置。

UICollectionView组件

UICollectionView包含以下主要组件。

  • UICollectionViewCell:与UITableViewCells一样,这些单元格是UICollectionView的子视图。
    我们的内容仅显示在这些单元格中。
    用户离开屏幕时,单元格已出队
  • 补充视图:它由其他重要组成部分,例如标签,节的页眉和页脚,用于定义和划分内容区域

UICollectionView示例

让我们利用情节提要板在视图控制器中实现"收藏夹视图"。

为此,将"集合视图"拖动到"视图控制器"上,然后将其拖动以覆盖导航列下方的全屏。

将背景色更改为黑色,以使"收藏夹视图"的边界完全可见。

如下图所示,UICollectionViewCell在CollectionView的左上方可见。

UICollectionView单元太小。
让我们在右侧面板的属性检查器中将其放大。
我们在下图中显示了如何执行此操作。

我们还定义了单元标识符,该标识符将在ViewController.swift文件中使用。

像往常一样,我们需要控制单击并将"集合视图"拖动到停靠ViewController按钮上,以连接数据源和委托。
这也可以在代码中完成,但是我们更喜欢在这里进行。

情节提要已准备就绪。
我们将从助手编辑器将集合视图出口添加到ViewController.swift,如下所示。

下面给出了" ViewController.swift"。

import UIKit

let reuseIdentifier = "CellIdentifer";

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

  @IBOutlet var collectionView: UICollectionView!
  
  override func viewDidLoad() {
      super.viewDidLoad()
      //Do any additional setup after loading the view, typically from a nib.
  }

  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      //Dispose of any resources that can be recreated.
  }
  
  //UICollectionViewDelegateFlowLayout methods
  func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat
  {
      
      return 4;
  }
  func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat
  {
      
      return 1;
  }
  
  
  //UICollectionViewDatasource methods
  func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
      
      return 1
  }
  
  func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      
      return 100
  }
  
  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
  
      cell.backgroundColor = self.randomColor()
      
      
      return cell
  }
  

  //custom function to generate a random UIColor
  func randomColor() -> UIColor{
      let red = CGFloat(drand48())
      let green = CGFloat(drand48())
      let blue = CGFloat(drand48())
      return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
  }

}

一次要消化很多代码!让我们分解一下。

除了UIViewController之外,我们还添加了所有使用的协议方法。

注意:我们选择了UIViewController作为父类,并其中添加了集合视图。
还有一个UICollectionViewController类,可以通过将ViewController对象的所有实例(包括情节提要中的屏幕)替换为UICollectionViewController来使用。

现在,在我们研究UICollectionViewDelegateFlowLayout方法之前,一旦切换到情节提要并聚焦集合视图并将项目数更改为2,便会看到如下图所示的图像。

我们需要自定义单元格之间的间距,以使其看起来更干净。
因此,实现了两种方法。
返回的值是CGFloat值。
在尝试了不同的值之后,我们发现上面给出的值最适合当前的iOS应用。

UICollectionViewDatasource方法用于定义节数和项目数。
我们在这里随机分配了数字。

函数collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)-> UICollectionViewCell与TableView示例中的类似。
我们使用randomColor()函数为每个单元格分配了一个随机的背景值,方法是选择一个随机的" RG B"并返回UIColor实例。