swift UICollectionView 區(qū)頭以及cell使用

效果如下:



代碼如下:

//設(shè)置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:--設(shè)置區(qū)頭

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="我的資產(chǎn)"

}else{

headerView.titleLabel.text="美團(tuán)服務(wù)"

}

returnheaderView

}

}else{

varfooterView =UICollectionReusableView()

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

returnfooterView

}

}

//設(shè)置區(qū)頭高度

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

ifsection==0{

returnCGSize(width:screenWidth,height:190)

}else{

returnCGSize(width:screenWidth,height:50)

}

}

//設(shè)置cell的大小

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

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

returnCGSize(width:w,height:w)

}

//設(shè)置間距

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:--設(shè)置導(dǎo)航

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)

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容