您無法在界面構建器中拖放並獲取集合視圖的標題視圖。你必須另外實現返回UICollectionReusableView的viewForSupplementaryElementOfKind方法。在使用集合可重用視圖時,我們必須以不同的方式處理該視圖,類似於我們爲可重用細胞所做的事情。
要遵循的步驟。
- 創建該標頭視圖一個類(HeaderViewExample)。
- 將類(HeaderViewExample)分配給剛添加到界面構建器中的可重用視圖。
- 爲可重用的視圖提供可重用的標識符(HeaderViewExample)。
- 現在您將標籤或按鈕添加到可重複使用的視圖中,併爲類HeaderViewExample中的這些插座創建插座。
(注:當使用可重複使用的意見,不要直接在控制器中創建網點)
立即更新與下面的代碼你CollectionViewController。
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath:
NSIndexPath) -> UICollectionReusableView {
var reusableView = UICollectionReusableView()
if kind == UICollectionElementKindSectionHeader {
guard let view = collectionView?.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: String(HeaderViewExample), forIndexPath: indexPath) as? HeaderViewExample else { return reusableView }
view.label = "Test String"
view.backgroundColor = UIColor.redColor()
} else {
assert(false, "Unexpected element kind")
}
return reusableView
}
我建議您在底部使用滾動視圖,並在頂部設置視圖和集合視圖。這將在未來給你帶來更好的效果 –
蘋果不贊成這個解決方案,因爲兩個卷軸重疊,即使我禁用collectionView的滾動屬性另一個問題是我沒有我的contentView(CollectionView的高度是動態的)的確切高度說我的scrollView和這將導致在IB @SivajeeBattina – Omnia
錯誤不,你可以找出收集視圖的高度。使用collectionView.ContentSize屬性來獲取它的高度。 –