最近写swift项目的时候利用懒加载方式加载collectionview,因为没有特殊的要求,就简单些了代码如下:
private lazy var collectionView:UICollectionView = {
let layout = UICollectionViewLayout.init()
let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.backgroundColor = UIColor.white
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = false
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: UICollectionViewCellID)
return collectionView
}()
一眼看去没啥问题,但是运行起来就不走代理方法,也没有报错,一时懵逼。
再仔细看了代码发现把代码写错了:
把UICollectionViewFlowLayout写成了UICollectionViewLayout
修改回来就好了!记录一下,希望能帮助到你
END.