效果如下:
代码如下:
//设置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)
}
}