swift4.0 CollectionView和自定义cell

原创文章,转载请获得本人同意

本文有snapkit三方布局的使用,类似于masonry,自己了解一下

另外用懒加载的方法添加了表头


一、CollectionView和协议实现

//

//  HomeViewController.swift

//  SwiftTest18-1-5

//

//  Created by QC on 2018/1/8.

//  Copyright © 2018年 BoYaXun. All rights reserved.

//



/*

 *本类主要实现collectionView

 *动态布局

 *这个类我还准备测试一下网络数据请求

 */



import UIKit


class HomeViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {



    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor =RGBA(r:242, g: 242, b: 242, a: 1)

        // Do any additional setup after loading the view.

        self.title ="首页"

        myCollectionView()

        

        

        

    }

    

    

    func myCollectionView() {

        

        

        //自定义item的FlowLayout

        let flowLayout =UICollectionViewFlowLayout()

        //设置item的size

        flowLayout.itemSize = CGSize.init(width:CKWIDTH/4,height:CKWIDTH/4)

        //设置item的排列方式

        flowLayout.scrollDirection =UICollectionViewScrollDirection.vertical

        //设置item的四边边距

        flowLayout.sectionInset =UIEdgeInsetsMake(10,0, 10, 0)

        //列间距

        flowLayout.minimumLineSpacing =0

        //行间距

        flowLayout.minimumInteritemSpacing =0

        //设置页头尺寸

        //flowLayout.headerReferenceSize = CGSize.init(width:,height:)

        //设置页尾尺寸

        //flowLayout.footerReferenceSize = CGSize.init(width: , height: )

        //自定义uicollectionview的位置

        

        let collectionView = UICollectionView(frame: CGRect.init(x:0, y:64, width: CKWIDTH, height: CKHEIGHT - 64),collectionViewLayout:flowLayout)

        self.view.addSubview(collectionView)


        

        //设置背景颜色

        collectionView.backgroundColor =RGBA(r:242, g: 242, b: 242, a: 1)

        //设置垂直滚动是否滚到item的最底部

        collectionView.alwaysBounceVertical =true

        //设置水平滚动是否滚到item的最右边

        collectionView.alwaysBounceHorizontal =true

        //设置数据源对象

        collectionView.dataSource =self

        //设置代理对象

        collectionView.delegate =self

        //设置uicollectionView的单元格点击

        collectionView.allowsSelection =true

        //设置uicollectionView的单元格多选

        collectionView.allowsMultipleSelection =false

        //开启uicollectionView的分页显示效果

        collectionView.isPagingEnabled =true

        //注册uicollectionviewcell

        collectionView.register(HomeCollectionViewCell.self, forCellWithReuseIdentifier:"cell")

        

        

        collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier:"reusable")

        

    }

    func numberOfSections(in collectionView:UICollectionView) ->Int {

        return 1

    }

    

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

        return 3

    }

    

    func collectionView(_ collectionView:UICollectionView, cellForItemAt indexPath:IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"cell", for: indexPath)

        

        return cell

        

    }

    //分区头高度

    func collectionView(_ collectionView:UICollectionView, layout collectionViewLayout:UICollectionViewLayout, referenceSizeForHeaderInSection section:Int) -> CGSize {

        return CGSize.init(width: CKWIDTH, height:200)

    }

    

    //分区头设置

    func collectionView(_ collectionView:UICollectionView, viewForSupplementaryElementOfKind kind:String, at indexPath:IndexPath) ->UICollectionReusableView {

        

        var reusableview:UICollectionReusableView!

        if kind ==UICollectionElementKindSectionHeader{

            

            reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier:"reusable", for: indexPath)

            reusableview.addSubview(headerImage)


        }

        return reusableview


    }

    

    

    

    //懒加载一个imageView

    lazy var headerImage:UIImageView = {

        

        let headerImage = UIImageView()

        headerImage.setImageWith(nil, placeholder:UIImage(named:"cat"))

        headerImage.frame = CGRect.init(x:0,y:0,width:CKWIDTH,height:200)

        return headerImage


    }()

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    


    /*

    // MARK: - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the new view controller using segue.destinationViewController.

        // Pass the selected object to the new view controller.

    }

    */


}




二 、自定义cell

//

//  HomeCollectionViewCell.swift

//  SwiftTest18-1-5

//

//  Created by QC on 2018/1/11.

//  Copyright © 2018年 BoYaXun. All rights reserved.

//


import UIKit


class HomeCollectionViewCell: UICollectionViewCell {

    

    var  titleLabel:UILabel?

    var  imageView:UIImageView?

    

    

    

    override init(frame: CGRect) {

        super.init(frame: frame)

        

        let cellWidth:CGFloat =self.frame.size.width

        let cellHeight:CGFloat =self.frame.size.height

        self.imageView =UIImageView()

        self.addSubview(self.imageView!)

        self.imageView?.snp.makeConstraints({ (make)in

            

            make.centerX.equalTo(self)

            make.centerY.equalTo(self).offset(-10)

            make.width.equalTo(cellWidth/3)

            make.height.equalTo(cellHeight/3)

            

        })

        self.imageView?.setImageWith(nil, placeholder:UIImage(named:"cat"))

        

        self.titleLabel =UILabel()

        self.addSubview(self.titleLabel!)

        self.titleLabel?.snp.makeConstraints({ (make)in

            make.centerX.equalTo(self)

            make.top.equalTo((self.imageView?.snp.bottom)!).offset(10)

            make.width.equalTo(width)

            make.height.equalTo(13)

        })

        self.titleLabel?.font =UIFont.systemFont(ofSize:13)

        self.titleLabel?.text ="新闻中心"

        self.titleLabel?.textAlignment =NSTextAlignment.center

 

        

    }

    

    

    

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

}


    

    

    







  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值