使集合观点正确还有很长的需要
步骤名单我将努力使这一尽可能明确如此忍受我就可以了
选择指南导向
所以假设你想要基于某些变量让所有单元格不同并且数组中的某些存储数据让我们开始工作,因为我们已经有相当一部分了。
第1步。
打开你的故事板,并在视图控制器你想添加这个的CollectionView创建一个新的.swift文件这些东西添加到它
import Foundation
import UIKit
class YourViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
}
(假设您是熟悉创建类和和在故事板捆扎这些类ViewControllers)
步骤2.
导航转移到身份检查卡舌O n个右手属性检查器选项卡,您的viewController设置为我们只是在你想找到并拖动到的CollectionView您所选择的viewController(确保底部创建
步骤3
现在下降的类其收集视图而不是收集视图控制器)
步骤4。
在这里你可以设置你的细胞的大小和每个单元等(以上在属性检查器)
步骤5
一旦你有你想要的一切你想要的间距某些约束添加到该新创建的CollectionView
在容器水平-add一个中心
-add一个尾随/领先的C onstraints到的的viewController
边缘在容器垂直 - 添加中心
步骤6
现在
了的CollectionView是你需要做一些编码的东西
保持设置控制并从collectionView(不是collectionViewCell) 拖动到viewController(在左侧或在viewController的三个符号所在的顶部)添加委托和数据源,弹出选项(这些都是重要的,如果你不这样做它不会工作)
步骤7
开拓助理编辑和保持控制,并从的CollectionView单击并拖动您的viewController类文件使它成为@IBOutlet并将其命名为任何你想要的,你应该有这样的事情
class YourViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var YourCollectionView: UICollectionView!
}
步骤8
现在我TS时间真正棘手的东西,你要创建一个新的文件,并选择可可触摸类
确保您有它的类型UICollectionViewCell的一个子类,并单击还创建XIB文件复选框您可以将其命名为
第9步。
现在这就是失控的方式时间为一些用于设置头所需的其他事情交给yourViewController类文件和该位添加到它
override func viewDidLoad() {
super.viewDidLoad()
//or whatever you named the @IBOutlet earlier
YourCollectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell")
// Do any additional setup after loading the view.
}
现在如果你建立并运行现在的应用程序将多数民众赞成崩溃因为我们之前创建的CollectionViewCell没有被赋予重用标识符
将此头部固定到您刚刚创建的CollectionViewCell XIB并在右侧进行(请确保在执行此操作之前选择了CollectionViewCell或您不会看到任何东西)
在上面添加任何你想要重用标识符名称,但要确保其相关负责人回didmove(toview:__)方法在你的viewController文件在这条线在这里
YourCollectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell")
确保forCellWithReuseIdentifier串相同的重用标识符你只需输入你的XIB细胞
步骤10
现在
你可以定制你的细胞在厦门国际银行文件(你可以改变大小和一切DD图像和您添加到细胞是开拓为CollectionViewCell XIB和@IBOutlet的助理编辑每个标签/图像后标签等)给他们,你可以轻松地引用后
步骤11
相关名称
后脑勺到您的viewController文件,并在这样
struct cellData {
//here you need to create a *let instance* for every image/label you have in your CollectionViewCell XIB
//this is neccesary for determining how to seperate the cells
let cell : Int!
//labels will not be declared as a UILabel but as a String like this
let label : String!
//images will be declared like this
let Image : UIImage!
}
class YourViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
步骤12
现在我们已完成了安装我们现在必须移动到的定制部分的顶部创建一个struct代码
创建类内的空数组
class YourViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var YourCollectionView: UICollectionView!
/* array here */
var array = []
override func viewDidLoad() {
super.viewDidLoad()
YourCollectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell")
// Do any additional setup after loading the view.
}
}
的数据填充数组中你的结构是这样
var array = [cellData(cell : 1,label : "",image : UIImage(named: ""))]
确定,所以让我们来看看这个在这里,我们将宣布所有不同的标签和图像的结构(应该有一个标签,或者你放在你CollectionViewCell XIB每个标签或图像的图像)的功能
现在是时候所述的CollectionView
步骤13,两者均。
这整个过程中,你可能已经得到一个错误,但我们要解决它
添加这三个函数到您的viewController功能
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
}
-numberOfSections意味着细胞的许多不同的部分怎么办你想
-numberOfItemsInSection意味着多少个单体电池,你在你的部分要
步骤14
的CollectionView是决定要添加它
if array[indexPath.row].cell == 1 {
//make sure this is the reuse identifier that you have set in your CollectionViewCell XIB
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
cell.image.image = array[indexPath.row].image
cell.label.text = array[indexPath.row].label
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
return cell
}
步骤15
这里面的代码显示在每个单元什么,那是,如果你想添加更多的细胞funtion不同的数据只是把更多的进入你的阵列
var array = [cellData(cell : 1,label : "",image : UIImage(named: "")),
cellData(cell : 2,label : "",image : UIImage(named: "")),
cellData(cell : 3,label : "",image : UIImage(named: ""))]
并确保它被装在你的CollectionView功能
if array[indexPath.row].cell == 1 {
//make sure this is the reuse identifier that you have set in your CollectionViewCell XIB
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
cell.image.image = array[indexPath.row].image
cell.label.text = array[indexPath.row].label
return cell
}
if array[indexPath.row].cell == 2 {
//make sure this is the reuse identifier that you have set in your CollectionViewCell XIB
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
cell.image.image = array[indexPath.row].image
cell.label.text = array[indexPath.row].label
return cell
}
if array[indexPath.row].cell == 3 {
//make sure this is the reuse identifier that you have set in your CollectionViewCell XIB
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
cell.image.image = array[indexPath.row].image
cell.label.text = array[indexPath.row].label
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
return cell
}
确保您的数组中你细胞诠释设置为增加整数值或细胞不会加载还可以自定义在每个单元只是填补空白的阵列中的数据
var array = [cellData(cell : 1,label : "Blank here",image : UIImage(named: "Blank here"))]
如果您有任何问题,请发表评论
您可以分享演示项目吗? –
你在storyBoard中设置了这个collectionView吗?如果你有,那么我可以帮助你,否则我真的不知道如何以编程方式做的东西**建议**尝试添加一个NSConstraint(你必须在其他地方做更多的研究)的宽度编程 –