//
// ViewController.swift
// CollectionView_test
//
import UIKit
private let CollectionCellId = "CollectionCell"
class ViewController: UIViewController {
//懒加载 collectionView
fileprivate lazy var collectionView : UICollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: CustomFlowLayout())
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.frame = view.bounds
collectionView.backgroundColor = UIColor(red: 244/255.0, green: 244/255.0, blue: 244/255.0, alpha: 1)
collectionView.dataSource = self;
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: CollectionCellId) //注册cell
}
}
//MARK:- UICollectionViewDataSource
extension ViewController : UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100;
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionCellId, for: indexPath)
let R = (arc4random() % 256)
let G = (arc4random() % 256)
let B = (arc4random() % 256)
cell.backgroundColor = UIColor(red: CGFloat(R)/255, green: CGFloat(G)/255, blue: CGFloat(B)/255, alpha: 1.0)
return cell
}
}
//MARK:- 自定义流水布局
class CustomFlowLayout: UICollectionViewFlowLayout {
//重写 prepare()方法
override func prepare() {
super.prepare()
itemSize = CGSize(width: 120 , height: 120)
minimumLineSpacing = 2 //行间距
minimumInteritemSpacing = 2 //列间距
scrollDirection = .horizontal //滚动方向
collectionView?.isPagingEnabled = true //设置分页效果
collectionView?.showsHorizontalScrollIndicator = false //隐藏水平滚动条
}
}
Swift 3 - 创建collectionView(自定义流水布局)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Github地址:-CollectionViewLayout-CollectionViewFlowLayout- ...
- Github地址:-CollectionViewLayout-CollectionViewFlowLayout- ...