您无法在界面构建器中拖放并获取集合视图的标题视图。你必须另外实现返回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属性来获取它的高度。 –