首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RxDataSources headerView和footerView

RxDataSources headerView和footerView
EN

Stack Overflow用户
提问于 2021-01-26 01:04:41
回答 1查看 1.4K关注 0票数 1

Problem:在从网络服务检索结果之前,不会显示CollectionView页眉和页脚。它们是在从服务中检索项目之后显示的。

我想要的结果::在从api调用检索项之前显示CollectionView页眉和页脚

我尝试过的:使用不同的数据源,在初始viewDidLoad上使用页眉和页脚,并在加载结果时将数据源设置为零。不起作用

My (不太优雅)解决方案:在viewDidLoad上添加headerView和footerView子视图,当结果来自服务时用collectionView替换它们。

代码语言:javascript
复制
itemsLoading.drive(onNext: { loading in 
 if loading {
   view.addSubview(headerView)
   view.addSubview(footerView) 
} else {  
  headerView.removeFromSuperview()
  footerView.removeFromSuperView()
  view.addSubview(collectionView)

对这个问题有更好的解决方案吗?

我的代码:只留下了与Rxswift/RxDataSources相关的部分

代码语言:javascript
复制
struct Input { 

let id: Driver<String>

} 

struct Output {

let items: Driver<[Item]>

}

class ViewModel {

    func transform(input: Input) -> Output {

        let items = networkService.getItems(id: input.id) // async operation
 
        return Output(items: items)
    }
}
代码语言:javascript
复制
class ViewController: UIViewController {

    private let viewModel = ViewModel()
    private let disposeBag = DisposeBag()
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())

    override func viewDidLoad() {
        super.viewDidLoad()
        bindViewModel()
    }

    private func bindViewModel() {

        let dataSource = ViewController.dataSource()
        let id = .just(id)
        let input = Input(id: id)
        let output = viewModel.transform(input: input)

        output.items.map { items in
            items.map { item in
                SectionModel(model: "first Section", items: [item])
            }
        }
        .drive(collectionView.rx.items(dataSource: dataSource))
        .disposed(by: disposeBag)
    }


extension ViewController {

    typealias DataSource = RxCollectionViewSectionedReloadDataSource
    static func dataSource() -> DataSource<SectionModel<String, Item>> {
        .init(
            configureCell: { _, collectionView, indexPath, _ in
                let cell = collectionView.dequeueReusableCell(ofType: UICollectionViewCell.self, for: indexPath)
                return cell
            },
            configureSupplementaryView: { _, collectionView, kind, indexPath in
                switch kind {
                    case UICollectionView.elementKindSectionHeader:
                        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, kindType: SectionHeader.self, for: indexPath)
                        return header

                    case UICollectionView.elementKindSectionFooter:
                        let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, kindType: SectionFooter.self, for: indexPath)
                        return footer
                    default:
                        assert(false, "Unexpected element kind")
                }

            })
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-26 02:49:34

简单的解决方案(我觉得更优雅)是使用startWith发出一个SectionModel数组,每个数组都有0项,但是model包含正确的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65894545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档