【发布时间】:2016-10-08 00:57:11
【问题描述】:
我正在尝试以编程方式快速创建一个 uicollectionview,没有任何情节提要。我需要它是多个uicollectionview。 这是我的代码
class jobtypeViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource{
var collectionview1 = UICollectionView()
var collectionview2 = UICollectionView()
override func viewDidLoad() {
super.viewDidLoad()
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 100, height: 100)
collectionview1 = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
self.collectionview1.delegate = self
self.collectionview1.dataSource = self
collectionview1.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
self.view.addSubview(collectionview1)
collectionview2 = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
self.collectionview2.delegate = self
self.collectionview2.dataSource = self
collectionview2.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
self.view.addSubview(collectionview2)
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.collectionview1 {
return 5
}else{
return 4
}
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if collectionView == self.collectionview1 {
let cellA = collectionview1.dequeueReusableCellWithReuseIdentifier("Cell",forIndexPath: indexPath) as UICollectionViewCell
return cellA
}
else {
let cellB = collectionview2.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as UICollectionViewCell
return cellB
}
}
}
一切似乎都很好,我有布局、委托和数据源,但是当我运行它时,我收到此消息错误 Terminating app due to uncaught exception 'NSInvalidArgumentException',原因:'UICollectionView must be initialized with a non-nil layout parameter '。我错过了什么吗??
【问题讨论】:
-
我忘了补充说我为此创建了一个故事板,但我想用纯编码制作 uicollectionview(故事板中没有 collectionview)。我不知道这是否会有所不同。因为我仍然可以使用段控制实现它
标签: ios swift uicollectionview