2017-10-08 146 views
0

(不要把这个问题标为重复的,我读了很多问题,但是我没有找到我问题的答案。)'UICollectionView必须用非零布局参数初始化' - Swift 4

我创建了一个IUCollectionView类和一个UICollectionViewCell类。这里是集视图中的一个:

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") 
    } 
} 

然后,我有一个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 
    } 
} 

我在运行时出现此错误:UICollectionView must be initialized with a non-nil layout parameter 我重写所有的init,但似乎他们没有被调用。我阅读了有关类似问题的所有问题,但我没有找到任何答案。 (我正在使用Swift 4)。对不起如果我粘贴了很多代码,但我认为这是必要的。在此先感谢您的帮助。

回答

0

的问题是这一行:

var collectionView = NearbyPlacesUICollectionView() 

这最终调用错误的init方法,其中没有布局设置。您在NearbyPlacesUICollectionView课程中定义了3 init方法。使用导致布局设置的两者之一。

init方法:

init(collectionView: UICollectionView, frame: CGRect) 

是没有意义的。它应该是:

init(frame: CGRect) 

,因为你不使用(绝对不需要)的collectionView财产。

这也是奇怪的是,这个init

override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) 

调用你commonInit这反过来又创造并设置布局。但是您已经将布局设置为传递给init的布局。

+0

好吧,那么我只需要这个init:'override init(frame:CGRect,collectionViewLayout layout:UICollectionViewLayout)''。但是,如果我删除了我的'commonInit()'调用,我可以在哪里设置布局?谢谢。 –

+0

我试过所有初始化,但没有人工作! –

+0

如果您使用那个'init',则将所需布局传递给'init'。 – rmaddy