每次用都要搜索一圈相關(guān)用法,實(shí)在是不勝其煩,這里記錄一下.
定義好Identifier
private let cellReuseIdentifier = "tagCollectionViewCell"
private let headerReuseIdentifier = "headerReuseIdentifier"
UICollectionViewFlowLayout
collectionView的布局樣式類,這個(gè)玩意設(shè)置的靈活得多,不僅僅是布局,還可以實(shí)現(xiàn)很多奇特的效果,這里關(guān)注基本用法,在此就不再贅述.
初始化
let layout = UICollectionViewLayout()
let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
注冊headerview和cell
collectionView.registerNib(UINib.init(nibName: "TeaFriendsTagCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: cellReuseIdentifier)
collectionView.registerClass(headerView.classForCoder, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerReuseIdentifier)
相關(guān)協(xié)議
UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout
相關(guān)代理
collectionView datasource:
//數(shù)據(jù)的數(shù)量
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 0
}
//sections數(shù)量
func numberOfSectionsInCollectionView(collectionView:UICollectionView) ->Int {
return 0
}
collectionView delegate:
//點(diǎn)擊cell
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
}
CollectionView DelegateFlowLayout:
//headerview的Size
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSizeMake(ScreenWidth, 80 * HEIGHT_SCALE)
}
//這里是headerview
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader {
headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: headerReuseIdentifier, forIndexPath: indexPath) as! TagHeaderCollectionReusableView
headerView.pagedView.delegate = self
return headerView
}else {
return UICollectionReusableView()
}
}
//cell的Size
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let w = (ScreenWidth - 30)/2
return CGSizeMake(w, w)
}