到目前为止,无涯教程已经讨论了CollectionView,它是一种内容视图,用于表示项目集合,其中UICollectionViewCell表示项目。无涯教程已经使用CollectionViews创建了iOS应用程序。在本教程的这一部分中,无涯教程将使用CollectionViewController来管理collectionview。
可以将CollectionViewController定义为ViewController,它专门用于管理CollectionView。它是UICollectionViewController的实例,该实例继承了UIViewController类。
class UICollectionViewController : UIViewController
CollectionViewController实现UICollectionViewDelegate和UICollectionViewDataSource协议,以控制CollectionView的数据和用户交互。当CollectionView首次出现时,CollectionViewController会重新加载集合视图数据。每次显示视图时,它也会清除当前选择。但是,无涯教程可以通过将clearsSelectionOnViewWillAppear属性的值设置为false来更改此行为。
在Xcode中创建一个新的单视图iOS应用程序。无涯教程将在Xcode中获取以下自动生成的文件。
在这里,无涯教程需要在Storyboard中删除现有视图控制器,并通过在对象库中搜索它来添加Collection View Controller并将结果拖动到Storyboard。
这将在Storyboard中创建一个集合视图控制器,其中包含集合视图单元格。
在无涯教程的项目中,无涯教程还需要创建UICollectionViewController的子类,可以将其分配给Storyboard中的集合视图控制器。
要创建新类文件,请按 command + n ,然后选择"Cocoa Touch Class"选项。
这将打开以下图像中显示的窗口。输入类的名称,然后从下拉列表中选择父类作为UICollectioViewController。
在这里,无涯教程创建了一个类MyCollectionViewController,它是UICollectionViewController的子类。将此类分配给Storyboard中的集合视图控制器。
Collection View Controller包含屏幕上可见的各种特定组件。
CollectionView - Collection View Controller提供了一个内置的集合视图,带有集合视图单元格。可以使用UICollectionViewController子类中使用CollectionView属性访问此CollectionView。它是继承UIView的UICollectionView类的一个实例。
CollectionViewCell - Collection View Cell显示Collection View Controller的实际内容。它包含一个内容视图,其类型UIView无涯教程可以添加自定义子视图。它是UICollectionViewCell类的一个实例。
NavigationBar - 导航栏显示在导航控制器中的ViewController的顶部。它包含标题和栏按钮项目。它是UINavigationBar类的一个实例。
UICollectionViewController包含以下属性。
SN | Property | Description |
---|---|---|
1 | var collectionView: UICollectionView! | 它表示由视图控制器管理的集合视图对象。 |
2 | var collectionViewLayout: UICollectionViewLayout | 它表示用于初始化集合视图控制器的布局对象。 |
3 | var clearsSelectionOnViewWillAppear: Bool | 这是一个布尔值,指示当视图将要出现在屏幕上时是否应清除选择。 |
4 | var installsStandardGestureForInteractiveMovement: Bool | 布尔值,指示收集视图控制器是否安装了标准手势识别器来驱动重新排序过程。 |
5 | var useLayoutToLayoutNavigationTransitions: Bool | 布尔值,使用过渡效果进入控制器。 |
示例
在此示例中,无涯教程将创建一个简单的集合视图控制器,无涯教程将为每个CollectionView单元分配不同的颜色。
链接:https://www.learnfk.comhttps://www.learnfk.com/ios/ios-collectionviewcontroller.html
来源:LearnFk无涯教程网
在这里,无涯教程将搜索对象库中的集合视图控制器并将结果拖动到Storyboard。这将在Storyboard中创建一个CollectionViewController,使用CollectionView Prototype单元格。
现在,无涯教程将创建UICollectionViewController的子类,并将此类命名为MyCollectionViewController。
MyCollectionViewController.swift
import UIKit private let reuseIdentifier = "cell" class MyCollectionViewController: UICollectionViewController { var colorArr = Array<UIColor>() override func viewDidLoad() { super.viewDidLoad() //Uncomment the following line to preserve selection between presentations //self.clearsSelectionOnViewWillAppear = false //Register cell classes self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) colorArr = [UIColor.red,UIColor.black,UIColor.yellow,UIColor.blue,UIColor.gray,UIColor.green,UIColor.brown,UIColor.cyan,UIColor.darkGray] //Do any additional setup after loading the view. } override func numberOfSections(in collectionView: UICollectionView) -> Int { //#warning Incomplete implementation, return the number of sections return 3 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { //#warning Incomplete implementation, return the number of items return colorArr.count } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) //Configure the cell cell.contentView.backgroundColor = colorArr[indexPath.row] return cell } }
输出
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)