且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Swift 3.0 中的 UICollectionViewController 错误:必须使用非零布局参数进行初始化

更新时间:2022-04-13 00:22:37

谢谢!我采纳了你的建议.以下是有效的代码:

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

再次,非常感谢你们.祝你有美好的一天:D

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