美文网首页
swift UICollectionView 区头以及cell使

swift UICollectionView 区头以及cell使

作者: 今天天气很好嗯 | 来源:发表于2017-11-22 10:31 被阅读0次

    效果如下:

    代码如下:

    //设置layout

    letlayout=UICollectionViewFlowLayout()

    layout.footerReferenceSize=CGSize(width:screenWidth,height:20)

    layout.minimumLineSpacing=magin

    layout.minimumInteritemSpacing=0

    collectionView=UICollectionView(frame:CGRect(x:0,y:0,width:screenWidth,height:screenHeight), collectionViewLayout: layout)

    collectionView.delegate=self

    collectionView.dataSource=self

    collectionView.backgroundColor=UIColor.init(red:239/255.0, green:239/255.0, blue:239/255.0, alpha:1.0)

    self.view.addSubview(collectionView)

    collectionView.register(SZMineCollectionCell.self, forCellWithReuseIdentifier:collectionID)

    collectionView.register(SZMineCollectionHeader.self, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier:headerID)

    collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionFooter, withReuseIdentifier:footerID)

    collectionView.register(SZMineCollectionInfoHeader.self, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier:infoHeaderID)

    collectionView.alwaysBounceVertical=true

    // MARK:--UICollectionView delegate and dataSource

    funcnumberOfSections(in collectionView:UICollectionView) ->Int{

    return3

    }

    funccollectionView(_collectionView:UICollectionView, numberOfItemsInSection section:Int) ->Int{

    ifsection==0{

    return0

    }

    elseifsection==1{

    returnmyAssetLists.count

    }else{

    returnmtServiceLists.count

    }

    }

    // TODO:--设置区头

    funccollectionView(_collectionView:UICollectionView, viewForSupplementaryElementOfKind kind:String, at indexPath:IndexPath) ->UICollectionReusableView{

    ifkind==UICollectionElementKindSectionHeader{

    ifindexPath.section==0{

    varheaderView =SZMineCollectionInfoHeader()

    headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier:infoHeaderID, for: indexPath)as!SZMineCollectionInfoHeader

    returnheaderView

    }else{

    varheaderView =SZMineCollectionHeader(frame:CGRect(x:0,y:0,width:screenWidth,height:50))

    headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier:headerID, for: indexPath)as!SZMineCollectionHeader

    ifindexPath.section==1{

    headerView.titleLabel.text="我的资产"

    }else{

    headerView.titleLabel.text="美团服务"

    }

    returnheaderView

    }

    }else{

    varfooterView =UICollectionReusableView()

    footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier:footerID, for: indexPath)

    returnfooterView

    }

    }

    //设置区头高度

    funccollectionView(_collectionView:UICollectionView, layout collectionViewLayout:UICollectionViewLayout, referenceSizeForHeaderInSection section:Int) ->CGSize{

    ifsection==0{

    returnCGSize(width:screenWidth,height:190)

    }else{

    returnCGSize(width:screenWidth,height:50)

    }

    }

    //设置cell的大小

    funccollectionView(_collectionView:UICollectionView, layout collectionViewLayout:UICollectionViewLayout, sizeForItemAt indexPath:IndexPath) ->CGSize{

    letw :CGFloat= (screenWidth-magin*CGFloat(4) )/4.0

    returnCGSize(width:w,height:w)

    }

    //设置间距

    funccollectionView(_collectionView:UICollectionView, layout collectionViewLayout:UICollectionViewLayout, insetForSectionAt section:Int) ->UIEdgeInsets{

    returnUIEdgeInsetsMake(magin,magin,magin,magin)

    }

    funccollectionView(_collectionView:UICollectionView, cellForItemAt indexPath:IndexPath) ->UICollectionViewCell{

    letcell :SZMineCollectionCell= collectionView.dequeueReusableCell(withReuseIdentifier:collectionID, for: indexPath)as!SZMineCollectionCell

    ifindexPath.section==0{

    letmodel=myAssetLists[indexPath.row]as!SZMineCollectionModel

    cell.cellTitle.text= model.itemTitle

    cell.cellDes.text= model.itemDes

    }else{

    letmodel=mtServiceLists[indexPath.row]as!SZMineCollectionModel

    cell.cellTitle.text= model.itemTitle

    cell.cellDes.text= model.itemDes

    }

    returncell

    }

    // MARK:--设置导航

    funcscrollViewDidScroll(_scrollView:UIScrollView) {

    letoffsetY :CGFloat= scrollView.contentOffset.y

    ifoffsetY<64{

    navBarView.backgroundColor=UIColor.clear

    }else{

    navBarView.backgroundColor=UIColor.init(red:72/255.0, green:183/255.0, blue:159/255.0, alpha:1.0)

    }

    }

    相关文章

      网友评论

          本文标题:swift UICollectionView 区头以及cell使

          本文链接:https://www.haomeiwen.com/subject/umeemxtx.html