UIcollectionView基本使用
2636 ワード
使うたびに1周検索しなければならない使い方は、本当に煩わしいので、ここに記録しておきます.
Identifierを定義
UICollectionViewFlowLayout
collectionViewのレイアウトスタイルクラスは、レイアウトだけでなく、多くの奇抜な効果を実現するために、この基本的な使い方に注目し、ここでは説明しない.
初期化
headerviewとcellの登録
関連プロトコル
関連エージェント
Identifierを定義
private let cellReuseIdentifier = "tagCollectionViewCell"
private let headerReuseIdentifier = "headerReuseIdentifier"
UICollectionViewFlowLayout
collectionViewのレイアウトスタイルクラスは、レイアウトだけでなく、多くの奇抜な効果を実現するために、この基本的な使い方に注目し、ここでは説明しない.
初期化
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)
関連プロトコル
UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout
関連エージェント
collectionView datasource:
//
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 0
}
//sections
func numberOfSectionsInCollectionView(collectionView:UICollectionView) ->Int {
return 0
}
collectionView delegate:
// 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)
}