こんばんは。
現在、下記の記事を参考にしてPinterestライクなUIを実装しています。
http://qiita.com/eKushida/items/8f8965467d098ca05120
その上で、上記記事中の記事をほぼそのまま活用することで
Piterestライクなグリッドレイアウトが実現できたのですが
これに対して、Headerをどのようにつければよいかわからず悩んでいます。
ViewController内にcollectionView.delegate = selfとした上で
下記のコードを記載しているのですが、そもそも呼ばれていないようです。
Swift
1//ViewController内コード 2 3 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 4 //省略 5 } 6 7 8 func collectionView(_ collectionView: UICollectionView, 9 layout collectionViewLayout: UICollectionViewLayout, 10 referenceSizeForHeaderInSection section: Int) -> CGSize{ 11 return CGSize(width: UIScreen.main.bouds.width, height: 400) 12 }
Viewのコードは以下のようになっており、CollectionViewをイニシャライズするときに
上記Qiitaのコードに記載のPinterestLayout.swiftのコードをそっくりもってきたPinterestLayout()
というClassをLayoutとして引数に当てています。
後半で、CollectionViewのCellとHeaderに当たるclassを登録しています。
Swift
1//View内のコード 2 3class PinterestView: UIView { 4 var collectionView! 5 6 override init(frame: CGRect) { 7 super.init(frame: frame) 8 collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height, collectionViewLayout: PinterestLayout()) 9 collectionView.register(PinterestCell.self, forCellWithReuseIdentifier: PinterestCell.identifier) 10 collectionView?.register(PinterestHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: PinterestHeader.identifier) 11 //以降省略 12 } 13 14} 15
その上で、通常の場合、UICollectionViewFlowLayoutをイニシャライズした上で
layout.headerReferenceSize = CGSize()といった記載をすることで、Headerの高さを
定義すると思うのですが、今回の場合
①UICollectionViewFlowLayoutを外部ファイルにおいてしまっている
②Headerの高さが中身に応じて動的に可変となる
となっているため、おそらくPinterestLayout()の中で何らかの記載をする必要があると考えています。
つきましては、PinterestLayout()内のどこにどのようなコードを書けば
Headerが正常に表示されるようになるのか、アドバイスいただけないでしょうか。
PinterestLayout()については、冒頭で記載したQiitaの記事中のコードと考えていただいて構いません。
よろしくお願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/29 01:30