【发布时间】:2019-09-16 10:45:43
【问题描述】:
我正在尝试编写一个自定义类来创建一个 UICollectionView,我打算从不同的类中创建多个实例。下面是我在自定义 UICollectionView 类中使用的代码:
import UIKit
class CustomCollectionView: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource {
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout: layout)
setupCollectionView(topEdgeInset: 20, bottomEdgeInset: 20, leftEdgeInset: 20, rightEdgeInset: 20, screenWdith: frame.width, screenHeight: frame.height, minimumCellsHorizontalSpacing: 20, minimumCellsVerticalSpacing: 20, numberOfCellsPerRow: 2, viewCollectionViewWillBeAddedTo: self, dataSource: self as! UICollectionViewDataSource, delegate: self as! UICollectionViewDelegate)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
convenience init(topEdgeInset: CGFloat, bottomEdgeInset: CGFloat, leftEdgeInset: CGFloat, rightEdgeInset: CGFloat, screenWdith: CGFloat, screenHeight: CGFloat, minimumCellsHorizontalSpacing: CGFloat, minimumCellsVerticalSpacing: CGFloat, numberOfCellsPerRow: CGFloat, viewCollectionViewWillBeAddedTo: UIView, dataSource: UICollectionViewDataSource, delegate: UICollectionViewDelegate) {
self.init(frame: viewCollectionViewWillBeAddedTo.bounds,collectionViewLayout: UICollectionViewFlowLayout())
setupCollectionView(topEdgeInset: topEdgeInset, bottomEdgeInset: bottomEdgeInset, leftEdgeInset: leftEdgeInset, rightEdgeInset: rightEdgeInset, screenWdith: screenWdith, screenHeight: screenHeight, minimumCellsHorizontalSpacing: minimumCellsHorizontalSpacing, minimumCellsVerticalSpacing: minimumCellsVerticalSpacing, numberOfCellsPerRow: numberOfCellsPerRow, viewCollectionViewWillBeAddedTo: viewCollectionViewWillBeAddedTo, dataSource: dataSource, delegate: delegate)
}
func setupCollectionView (topEdgeInset topInset: CGFloat, bottomEdgeInset bottomInset: CGFloat, leftEdgeInset leftInset: CGFloat, rightEdgeInset rightInset: CGFloat, screenWdith width: CGFloat, screenHeight height: CGFloat, minimumCellsHorizontalSpacing cellsHorizontalSpacing: CGFloat, minimumCellsVerticalSpacing cellsVerticalSpacing: CGFloat, numberOfCellsPerRow cellsPerRow: CGFloat, viewCollectionViewWillBeAddedTo hostView: UIView, dataSource: UICollectionViewDataSource, delegate: UICollectionViewDelegate) {
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
layout.minimumLineSpacing = cellsVerticalSpacing
layout.minimumInteritemSpacing = cellsHorizontalSpacing
let widthOfCollectionViewCell: CGFloat = width - (leftInset + rightInset + cellsHorizontalSpacing) / cellsPerRow
layout.itemSize = CGSize(width: widthOfCollectionViewCell, height: 100)
let myCollectionView = UICollectionView(frame: hostView.bounds, collectionViewLayout: layout)
myCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
myCollectionView.dataSource = dataSource
myCollectionView.delegate = delegate
hostView.addSubview(self)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 8
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath)
myCell.backgroundColor = .blue
return myCell
}
}
下面的类是我从上面的自定义 UICollectionView 类创建一个实例的地方:
class FirstItemInTabBarOpenRolledSections: UIViewController
{
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
_ = CustomCollectionView(topEdgeInset: 20, bottomEdgeInset: 20, leftEdgeInset: 20, rightEdgeInset: 20, screenWdith: self.view.frame.width, screenHeight: self.view.frame.height, minimumCellsHorizontalSpacing: 20, minimumCellsVerticalSpacing: 20, numberOfCellsPerRow: 2, viewCollectionViewWillBeAddedTo: self.view, dataSource: self as! UICollectionViewDataSource, delegate: self as! UICollectionViewDelegate)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("User tapped on itme \(indexPath.row)")
}
}
但是,每次我运行应用程序时,它都会崩溃并且我收到一条错误消息:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“UICollectionView 必须使用非零布局参数初始化”
我不明白我的布局没有初始化??谁能指出问题出在哪里?
【问题讨论】:
-
我回滚了您上次的大编辑,因为发布工作代码代替原始代码会使整个问题变得不必要,并且不需要任何答案。如果有的话,请使用您的工作代码发布您自己的答案。
标签: swift layout uicollectionview