您的位置:首页 > 移动开发 > Swift

UICollectionView swift2模版

2016-02-18 01:16 441 查看
class testViewController:BaseViewController,UICollectionViewDataSource, UICollectionViewDelegate , UICollectionViewDelegateFlowLayout{
lazy var myCollectionView:UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 1.5  //上下间隔
layout.minimumInteritemSpacing = 1 //左右间隔
let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout:layout)
collectionView.backgroundColor = UIColor.whiteColor()
return collectionView
}()

override func viewDidLoad() {
super.viewDidLoad()
//加载页面元素
self.addSubView()
self.makeConstraints()
self.myCollectionView.reloadData()
}

func addSubView(){
self.view.backgroundColor = UIColor(rgba: "#F1F1F1")
self.myCollectionView.delegate = self
self.myCollectionView.dataSource = self

//注册header
self.myCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "TESTHeader")

self.myCollectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "TESTCell")
self.view.addSubview(myCollectionView)
}

//增加约束
func makeConstraints(){
//Add Constraints
myCollectionView.snp_makeConstraints{make in
make.top.bottom.left.right.equalTo(self.view)
}
}

//MARK: - CollectionView 代理方法
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//分栏数量
return 10
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//每个分栏中Cell的个数
return 2
}

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
var reuseView:UICollectionReusableView
reuseView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "TESTHeader", forIndexPath: indexPath)
reuseView.backgroundColor = UIColor.blackColor()
return reuseView
}

///cell内容
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//返回复用的cell
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("TESTCell", forIndexPath: indexPath)
if indexPath.row % 2 == 0 {
cell.backgroundColor = UIColor.redColor()
}else{
cell.backgroundColor = UIColor.blueColor()
}
return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
//返回每个cell的大小
return CGSize(width: KMainScreenWidth, height:150)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
var h:CGFloat = 15
return CGSizeMake(KMainScreenWidth,h)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
//返回sectionview的大小
var h:CGFloat = 50
return CGSizeMake(KMainScreenWidth,h)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
return UIEdgeInsets(top:0, left: 0, bottom:5, right: 0)
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
//Cell点击事件
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: