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

swift之UICollectionView的使用

2018-02-07 15:34 1116 查看
//组头

import UIKit

class LYBHomeconvienceCollectionHeaderViewCollectionReusableView:
UICollectionReusableView {

    override init(frame:
CGRect) {

        super.init(frame: frame)

       

        initViews()

    }

    func initViews(){

        backgroundColor=UIColor.blue

        addSubview(lab)

    }

    required init?(coder aDecoder:
NSCoder) {

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

    }

    lazy var lab:UILabel={

       let lbl=UILabel.init(frame:
CGRect.init(x:
0, y: 0, width:
WIDTH, height: self.frame.size.height))

       lbl.text="便民"

        return lbl

        

    }()

}
*****************设置cell

//便民板块的cell

import UIKit

class LYBHomeConvienceCollectionViewCell:
UICollectionViewCell {

    override init(frame:
CGRect) {

        super.init(frame: frame)

        initViews()

    }

    func initViews(){

        addSubview(imageV)

        addSubview(lab)

        lab.backgroundColor=UIColor.white

    }

    required init?(coder aDecoder:
NSCoder) {

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

        

    }

    

    func setcell(imageStr:String,titleStr:String) {

        imageV.image=UIImage.init(named: imageStr)

        lab.text=titleStr

    }

    lazy var imageV:UIImageView={

       let imagev=UIImageView.init(frame:
CGRect.init(x:
20, y: 20, width:
WIDTH/4-40, height:
WIDTH/4-40))

        

        return imagev

    }()

    

    lazy var lab:UILabel={

       let lbl=UILabel.init(frame:
CGRect.init(x:
0, y: WIDTH/4, width:
WIDTH/4, height:
30))

        lbl.textAlignment=NSTextAlignment.center

        lbl.font=UIFont.systemFont(ofSize:
13)

        return lbl

    }()

}

************************主控制器

import UIKit

class LYBConvinienceCollectionView:
UIView,UICollectionViewDelegate,UICollectionViewDataSource{

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

        return 1

    }

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

       

        return imageArr.count

    

    }

    

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

        let cell:LYBHomeConvienceCollectionViewCell=collectionview.dequeueReusableCell(withReuseIdentifier:
homeConvienceCollectionIdentifier, for: indexPath)
as!
LYBHomeConvienceCollectionViewCell

cell.setcell(imageStr:
imageArr[indexPath.item], titleStr:
titleArr[indexPath.item])

        return cell

    }

    func collectionView(_ collectionView:
UICollectionView, didSelectItemAt indexPath:
IndexPath) {

        collectionview .deselectItem(at: indexPath, animated:
true)

    }

    

    //UICollectionElementKindSectionHeader和 kind 比较来区分header 和footer

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

        var headerView :
LYBHomeconvienceCollectionHeaderViewCollectionReusableView = (collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier:
"homeCollectionviewHeader", for: indexPath
as IndexPath)
as?
LYBHomeconvienceCollectionHeaderViewCollectionReusableView)!

        //分区头

        if kind ==
UICollectionElementKindSectionHeader{

            headerView = (collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier:
"homeCollectionviewHeader", for: indexPath
as IndexPath)
as? LYBHomeconvienceCollectionHeaderViewCollectionReusableView)!

        }

            //分区尾

        else
if kind ==
UICollectionElementKindSectionFooter{

            

            

        }

      

        return headerView

    }

    ***********//组头的高度---这个方法不执行;换成flow.headerReferenceSize = CGSize.init(width: WIDTH, height: 50)就可以了

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

        

        return CGSize.init(width:
WIDTH, height: 50)

        

    }

注意:设置组头的注意事项:

1.当前控制器遵守协议(UICollectionViewDelegateFlowLayout);
2.注意设置头部的size;
3. 当设置size,依然无法显示的原因:
-> 不要使用代理方法,(- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout
*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section);
->换成使用直接赋值方法(flowLayout.headerReferenceSize = CGSizeMake(xx, xx);).
4.未显示的原因:代理方法是最后才执行的.所以当内部添加了header之后,还没有走代理方法执行size,因此不会显示. 

    ***************

    override init(frame:
CGRect) {

        super.init(frame: frame)

        initViews()

    }

    

    func initViews(){

        flow.itemSize=CGSize.init(width:
WIDTH/4, height:
WIDTH/4+30)

        flow.minimumLineSpacing=0

        flow.minimumInteritemSpacing=0

        addSubview(collectionview)

        collectionview.delegate=self

        collectionview.dataSource=self

        collectionview.backgroundColor=UIColor.white

*******flow.headerReferenceSize = CGSize.init(width: WIDTH, height: 50)//设置组头高度

        //注册cell

        collectionview .register(LYBHomeConvienceCollectionViewCell.classForCoder(),
forCellWithReuseIdentifier: homeConvienceCollectionIdentifier)

       

        //注册组头

       collectionview.register(LYBHomeconvienceCollectionHeaderViewCollectionReusableView.classForCoder(), forSupplementaryViewOfKind:
UICollectionElementKindSectionHeader, withReuseIdentifier:
"homeCollectionviewHeader")

    }

    

    required init?(coder aDecoder:
NSCoder) {

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

    }

    

    

    lazy var imageArr:[String] = {

       let imageAr=["lczq","banCredit","homeDaikuan","wfsc","sjcz","flowcz","mediaIcon","jtfk"]

        return imageAr

    }()

    

    lazy var titleArr:[String] = {

       let titleAr=["信用卡垫还","大额信用卡","贷款","商城","话费","流量","共享媒体","违章查询"]

        return titleAr

    }()

    

    lazy
var flow:UICollectionViewFlowLayout = {

        let flo=UICollectionViewFlowLayout.init()

        return flo

    }()

    

    lazy var collectionview:UICollectionView = {

      let collect=UICollectionView.init(frame:
CGRect.init(x:
0, y: 0, width:
WIDTH, height: (WIDTH/4+30)*2+50), collectionViewLayout:
flow)

       

        return collect

    }()

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: