我使用RxSwift,我设法创建了dataSource并正确检索了单元格。问题在于headerView部分。我创建了一个UICollectionReusableView类,从故事板附加插件。
问题是我使用collectionview.dequeueReusableSupplementaryView检索视图,但从不调用awakeFromNib!
这就是我设置collectionView
的方法collectionView.register(UINib(nibName: "CardView", bundle: Bundle.main), forCellWithReuseIdentifier: "cardView")
collectionView.register(AddNewCardCollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "AddNewCardCollectionHeaderView")
这是我的UICollectionReusableView类
class AddNewCardCollectionHeaderView: UICollectionReusableView {
@IBOutlet weak var collectionViewRecommendations: UICollectionView!
@IBOutlet weak var viewWrapperRecommendations: UIView!
override func awakeFromNib() {
super.awakeFromNib()
}
}
这是dataSource
let dataSource = RxCollectionViewSectionedReloadDataSource<SectionOfItems>(configureCell: { (datasource, collectionview, indexPath, i) -> UICollectionViewCell in
let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "cardView", for: indexPath) as! CardView
let card = self.getCard(atRow: indexPath.row, isCardRecommendation: false)
self.setCell(card:card,cell:cell)
return cell
}, configureSupplementaryView: { (datasource, collectionview, kind, indexPath) -> UICollectionReusableView in
let headerView = collectionview.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "AddNewCardCollectionHeaderView", for: indexPath) as! AddNewCardCollectionHeaderView
return headerView
})
由于未调用awakeFromNib,如果我执行headerView。 viewWrapperRecommendations它将崩溃为nil,但我需要访问headerView outlet。
但是,相反,调用了CardView(单元类)的awakeFromNib并且它完美地运行。
有任何帮助吗?谢谢!