UICollectionViewController Error in Swift 3.0: must be initialized with a non-nil layout parameter

后端 未结 5 835
清酒与你
清酒与你 2021-01-14 08:03

I\'m new to iOS development. I\'ve been learning Swift, and today, I tried using UICollectionViewController.

My code is as follows:

 class ViewContro         


        
相关标签:
5条回答
  • 2021-01-14 08:16

    This should work:

    lazy var collectionView: UICollectionView = {
         let layout = UICollectionViewFlowLayout()
         let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
         return collectionView
    }()
    
    0 讨论(0)
  • 2021-01-14 08:16

    Just change the order like below one :

    let layout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
    layout.itemSize = CGSize(width: 90, height: 120)
    colView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    self.view.addSubview(colView)
    colView.delegate   = self
    colView.dataSource = self
    colView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
    colView.backgroundColor = UIColor.white
    
    0 讨论(0)
  • 2021-01-14 08:25

    You can set your code like this:

    class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
        let cellId = "Cell"
    
        lazy var collectionView: UICollectionView = {
            let layout = UICollectionViewFlowLayout()
            // ... layout parameters
            let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
            cv.backgroundColor = .white
            cv.delegate = self
            cv.dataSource = self
            return cv
        }()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            view.addSubview(collectionView)
            // ... add your auto layout constraints to the collection view
            collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
        }
    
        // Delegate and Data Source Methods...
    
    }
    
    0 讨论(0)
  • 2021-01-14 08:26

    Thank you! I incorporated your suggestions. Below is the code that works:

    class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
        let cellId = "CellId"
        var colView: UICollectionView!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let layout = UICollectionViewFlowLayout()
            layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
            layout.itemSize = CGSize(width: 111, height: 111)
            
            colView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
            colView.delegate   = self
            colView.dataSource = self
            colView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
            colView.backgroundColor = UIColor.white
            
            self.view.addSubview(colView)
        }
    
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 21
        }
        
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath)
            cell.backgroundColor = UIColor.orange
            return cell
        }
    }
    
    

    Again, thank you guys very much. Have an awesome day :D

    0 讨论(0)
  • 2021-01-14 08:40
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        let cellSize = CGSize(width: 90, height: 120)
        //
        return CGSizeMake(cellSize)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    
        return UIEdgeInsetsMake(top: 20, left: 10, bottom: 10, right: 10)
    }
    
    0 讨论(0)
提交回复
热议问题