我真的无法在文档或其他地方找到这一点,但是是否有一种方法可以使用RxDatasources提供从nib加载的自定义页眉和页脚?
例如,我正在将这样的一个单元格排出队列:
let dataSource = RxTableViewSectionedAnimatedDataSource<CommentsSectionModel>(
configureCell: { dataSource, tableView, indexPath, item in
if let cell = tableView.dequeueReusableCell(withIdentifier:
item.cellIdentifier, for: indexPath) as? BaseTableViewCell{
cell.setup(data: item.model)
return cell
}
return UITableViewCell()
})我看不出configureCell (titleForHeaderInSection除外)有什么东西可以让我去排队/配置可重用的页眉/页脚(这是标准的viewForHeaderInSection和viewForFooterInSection委托方法提供的)。
发布于 2021-12-28 13:04:06
自定义页眉/页脚不是UITableViewDataSource接口的一部分,因此它不是RxDataSource可以提供的东西。
如果需要,您可以按照我关于如何将Swift代表转换为RxSwift可观测数据的文章,并为此创建一个表视图委托.它不是库的一部分,因为表视图委托不符合push接口。
extension Reactive where Base: UITableView {
var delegate: UITableViewDelegateProxy {
return UITableViewDelegateProxy.proxy(for: base)
}
var viewForHeaderInSection: Binder<[Int: UIView]> {
Binder(delegate) { del, value in
del.viewForHeaderInSection.accept(value)
}
}
var viewForFooterInSection: Binder<[Int: UIView]> {
Binder(delegate) { del, value in
del.viewForFooterInSection.accept(value)
}
}
}
final class UITableViewDelegateProxy
: DelegateProxy<UITableView, UITableViewDelegate>
, DelegateProxyType
, UITableViewDelegate {
public static func registerKnownImplementations() {
self.register { UITableViewDelegateProxy(parentObject: $0) }
}
static func currentDelegate(for object: UITableView) -> UITableViewDelegate? {
object.delegate
}
static func setCurrentDelegate(_ delegate: UITableViewDelegate?, to object: UITableView) {
object.delegate = delegate
}
init(parentObject: UITableView) {
super.init(
parentObject: parentObject,
delegateProxy: UITableViewDelegateProxy.self
)
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
viewForHeaderInSection.value[section]
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
viewForFooterInSection.value[section]
}
fileprivate let viewForHeaderInSection = BehaviorRelay<[Int: UIView]>(value: [:])
fileprivate let viewForFooterInSection = BehaviorRelay<[Int: UIView]>(value: [:])
}发布于 2022-11-16 03:32:55
@DanielT上面的答案适用于我的情况,但它只显示了1节页眉/页脚。我是这样更新的,它已经修好了:
var viewForHeaderInSection: Binder<[Int: UIView]> {
Binder(delegate) { del, value in
let newValue = del.viewForHeaderInSection.value.merging(value, uniquingKeysWith: {(_,new) in new})
del.viewForHeaderInSection.accept(newValue)
}
}
var viewForFooterInSection: Binder<[Int: UIView]> {
Binder(delegate) { del, value in
let newValue = del.viewForFooterInSection.value.merging(value, uniquingKeysWith: {(_,new) in new})
del.viewForFooterInSection.accept(newValue)
}
}https://stackoverflow.com/questions/70503608
复制相似问题