Gol*_*Joe 15 ios uicollectionview swift
我有一个UICollectionView具有节标题,但没有节页脚.因此,我没有明确的页脚视图.
Apple的文档说明了这一点You must not return nil from this method.
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: "MyHeaderView",
for: indexPath) as! MyHeaderView
switch indexPath.section
{
case 0:
headerView.label_title.text = "SOME HEADER"
case 1:
headerView.label_title.text = "ANOTHER HEADER"
default:
headerView.label_title.text = "UNKNOWN HEADER"
}
return headerView
default:
assert(false, "Unexpected element kind") // Not a good idea either
}
return nil // NOPE, not allowed
}
Run Code Online (Sandbox Code Playgroud)
Ahm*_*edr 12
以上都不适合我.在我的情况下,我UICollectionView
在同一个视图控制器中有两个对象.首先是水平的,选择一个项目显示UICollectionView
垂直的下方并包含节标题.
来自Apple文档:
此方法必须始终返回有效的视图对象.如果您不希望在特定情况下使用补充视图,则布局对象不应为该视图创建属性.或者,您可以通过将相应属性的isHidden属性设置为true或将属性的alpha属性设置为0来隐藏视图.要在流布局中隐藏页眉和页脚视图,还可以设置这些视图的宽度和高度到0.
所以headerView
像往常一样出列,因为如果你不这样做,只是返回一个UICollectionReusableView()
Xcode 实例就会抱怨标题视图没有出列.然后,如果你不想出于某种原因显示它(对我来说,直到用户从上部水平集合视图中选择一个项目) - 设置headerView
to 的宽度和高度0.0
并返回它.
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: collectionHeaderReuseIdentifier, for: indexPath)
if objects.isEmpty {
headerView.frame.size.height = 0.0
headerView.frame.size.width = 0.0
return headerView
}
// Configure the header view here if needed
return headerView
Run Code Online (Sandbox Code Playgroud)
就我个人而言,如果您没有告诉 UICollectionView 显示节页脚,那么它不会显示它们。所以可以放入preconditionFailure
那个委托方法。
我的想法是, UICollectionView 不会做你告诉它做的更多的事情。因此,如果您没有告诉 UICollectionView 显示部分页脚,那么它不会显示它们,并且preconditionFailure
对于那些页脚视图来说是安全的。但是,如果您在这种情况下发现崩溃,那么要么是 UIKit 错误,您应该为此提交雷达以告诉 Apple,要么是您的错误要求 UICollectionView 无意中显示部分页脚。
所以我的代码看起来类似于这个
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: "MyHeaderView",
for: indexPath) as! MyHeaderView
/* Configure your section header here */
return headerView
default:
preconditionFailure("Invalid supplementary view type for this collection view")
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4151 次 |
最近记录: |