【发布时间】:2022-11-08 16:09:20
【问题描述】:
我似乎找不到向我的 UICollectionView 添加页脚视图的方法。我通常使用 RxSwift,我尝试了非 RxSwift 方式,但页脚仍然不显示。
我所做的是通过 ViewModel 获取数据并将其提供给 ViewController 中的 CollectionView,如下所示:
viewModel.output.data.drive(self.usersCollectionView.rx.items(cellIdentifier: "user", cellType: UserCollectionFooterView.self)) {[weak self] row, data, cell in
guard let self = self else {return }
cell.set(user: data)
}.disposed(by: disposeBag)
我创建了一个UserCollectionFooterView 类并检查了故事板中的“Section Footer”。我在页脚视图中添加了一个按钮,并将 IBOutlet 链接到我的 UserCollectionFooterView 类。
import Foundation
import UIKit
class UserCollectionFooterView : UICollectionReusableView {
@IBOutlet weak var leaveButton: UIButton!
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
在我的 ViewController 中,我已经注册了这个类:
usersCollectionView.register(UserCollectionFooterView, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "UserCollectionFooterView")
我已经实现了这两个功能:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize(width: usersCollectionView.bounds.width, height: 100)
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
return collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "UserCollectionFooterView", for: indexPath)
}
此外,我的 ViewController 实现了UICollectionViewDelegate。
拜托,有人能告诉我我做错了什么以及如何使用 RxSwift 添加 footerView 吗?
最佳解决方案是为整个集合视图实现一个页脚,但由于我的集合视图中只有一个部分,它可以是一个部分的页脚视图。
谢谢你的帮助!
【问题讨论】:
标签: ios swift uikit storyboard reactive