【发布时间】:2020-04-05 18:11:13
【问题描述】:
我已经发疯了好几个小时,因为我无法解决这个问题。
我有一个集合视图,它可以有不同的部分和不同的编号。在每个项目。对于每个部分,我需要使用不同类型的部分标题。所以为此,我将使用UICollectionReusableView。但是我似乎无法通过UINib 注册来成功使用UICollectionReusableView 的自定义子类。
当我将可重用视图向下转换到我的子类时,会发生崩溃。喜欢:
let friendHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: "FriendHeaderView",
for: indexPath) as! FriendHeaderView
下面是sn-p的代码:
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
private let viewModel = ProfileViewModel()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
// more code
collectionView.register(UINib(nibName: "FriendHeaderView", bundle: nil),
forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
withReuseIdentifier: "FriendHeaderView")
}
}
现在是数据源实现:
extension ViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
// valid implementation
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// valid implementation
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// valid implementation
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionView.elementKindSectionHeader:
let friendHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "FriendHeaderView", for: indexPath) as! FriendHeaderView
// *** Crash happens here *** //
return friendHeader
default:
assert(false, "Invalid element type")
}
}
}
而且我不知道为什么还需要实现collectionView(_:layout:referenceSizeForHeaderInSection:)。所以这里是:
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
let size = CGSize(width: collectionView.bounds.width, height: 100)
return size
}
}
好的,现在进入正题:上面提到的崩溃不会发生 如果我不使用
as!运营商的话。好吧,如果我使用部分 来自故事板的标题而不是UINib注册,有 没有崩溃。
如果我需要多种类型的标题,那么我也不能使用情节提要方法或不使用向下转换方法,因为我需要将数据提供给这些标题。
如何使用从界面构建器构建的多个类型标题?
我已经根据上面所说的内容创建了demo project。如果有人有兴趣,请检查一下。
【问题讨论】:
标签: ios swift uicollectionview uicollectionreusableview