churabou / iOS-develop-blog

0 stars 0 forks source link

5月16日 #29

Open churabou opened 6 years ago

churabou commented 6 years ago

collectionView

class ZeroCollectionView: UICollectionView {

convenience init() {
    let layout = UICollectionViewFlowLayout()
    let size = (UIScreen.main.bounds.width / 3)
    layout.itemSize = CGSize(width: size, height: size)
    layout.minimumLineSpacing = 0
    layout.minimumInteritemSpacing = 0
    self.init(frame: .zero, collectionViewLayout: layout)
    dataSource = self
    register(BaseCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
}

}

extension ZeroCollectionView: UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    cell.contentView.backgroundColor = indexPath.row % 2 == 0 ? .red : .blue
    return cell
}

}