[英]'UICollectionView must be initialized with a non-nil layout parameter' - Swift 4
(Don't mark this question as duplicated. I read a lot of questions but I don't find the answer to my issue.) (不要将此问题标记为重复。我读了很多问题,但是找不到我问题的答案。)
I have created a IUCollectionView
class and a UICollectionViewCell
class. 我创建了IUCollectionView
类和UICollectionViewCell
类。 Here is the collection view one: 这是集合视图之一:
class NearbyPlacesUICollectionView: UICollectionView {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
commonInit()
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout: layout)
commonInit()
}
init(collectionView: UICollectionView, frame: CGRect) {
let layout = UICollectionViewLayout()
super.init(frame: frame, collectionViewLayout: layout)
}
func commonInit() {
allowsMultipleSelection = false
backgroundColor = .white
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: UIScreen.main.bounds.width * 0.8875, height: 90*3)
frame = UIScreen.main.bounds
collectionViewLayout = layout
register(NearbyPlacesUICollectionView.self, forCellWithReuseIdentifier: "nearbyPlaceCell")
}
}
Then I have a UIViewController
where I have this (I remove some code to make it shorter): 然后,我在其中有一个UIViewController
(我删除了一些代码以使其更短):
class NearbyPlacesViewController: UIViewController, UICollectionViewDataSource {
var collectionView = NearbyPlacesUICollectionView()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
view.addSubview(collectionView)
}
}
extension NearbyPlacesViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: NearbyPlacesUICollectionViewCell? = collectionView.dequeueReusableCell(withReuseIdentifier: "nearbyPlaceCell", for: indexPath) as? NearbyPlacesUICollectionViewCell
return cell!
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if let cell = cell as? NearbyPlacesUICollectionViewCell
{
_configureCell(cell, atIndexPath: indexPath)
}
}
fileprivate func _configureCell(_ cell: NearbyPlacesUICollectionViewCell, atIndexPath indexPath: IndexPath)
{
cell.queue.cancelAllOperations()
let operation: BlockOperation = BlockOperation()
operation.addExecutionBlock { [weak operation] () -> Void in
DispatchQueue.main.sync(execute: { [weak operation] () -> Void in
if let operation = operation, operation.isCancelled {
return }
... // cell code here
})
}
cell.queue.addOperation(operation)
}
func collectionView(_ collectionView: UICollectionView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = .clear
... // header code here
return view
}
}
I get this error when running: UICollectionView must be initialized with a non-nil layout parameter
I override all the init but it seems like they are not called. 运行时出现此错误: UICollectionView must be initialized with a non-nil layout parameter
我会覆盖所有init,但似乎未调用它们。 I read all questions about similar issues but I didn't find any answer. 我阅读了有关类似问题的所有问题,但没有找到任何答案。 (I'm using Swift 4). (我正在使用Swift 4)。 Sorry If I paste a lot of code but I think it's necessary. 抱歉,如果我粘贴很多代码,但是我认为这是必要的。 Thanks in advance for your help. 在此先感谢您的帮助。
The problem is this line: 问题是这一行:
var collectionView = NearbyPlacesUICollectionView()
This ends up calling the wrong init
method where no layout is set. 最终将导致调用错误的init
方法(未设置布局)。 You've defined 3 init
methods in your NearbyPlacesUICollectionView
class. 您已经在NearbyPlacesUICollectionView
类中定义了3个init
方法。 Use one of the two that causes the layout to be set. 使用导致设置布局的两种方法之一。
And this init
method: 这个init
方法:
init(collectionView: UICollectionView, frame: CGRect)
makes no sense. 没有意义。 It should be: 它应该是:
init(frame: CGRect)
since you don't use (and definitely don't need) the collectionView
property. 因为您不使用(而且绝对不需要) collectionView
属性。
It's also odd that this init
: 这个init
也很奇怪:
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout)
calls your commonInit
which in turn creates and set a layout. 调用您的commonInit
,后者依次创建并设置布局。 But you've already set the layout to the one passed to the init
. 但是您已经将布局设置为传递给init
的布局。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.