RxDataSources 多数据源

不多解释,直接上代码:

//接口返回数据模型
struct NewModel {
    var id: Int
    var title: String
    var content: String
    var hasMore: String
}

//设置多不一样模型
struct NewMoreModel {
    var id: Int
    var title: String
    var content: String
    var description: String
}
//单元格类型
enum NewModelsSectionItem {
    case single(model: NewModel)
    case more(model: NewMoreModel)
}
//主体
struct NewCellModel {
    var header: String?
    var items: [NewModelsSectionItem]
}

extension NewCellModel: SectionModelType {
    
    typealias Item = NewModelsSectionItem
    
    init(original: NewCellModel, items: [Item]) {
        self = original
        self.items = items
    }
}
// 数据赋值
dataSource = RxTableViewSectionedReloadDataSource(configureCell: { (_, _, indexPath, item) -> UITableViewCell in

    switch item {
    case let .single(model):
        let cell = UITableViewCell.init(style: .value1, reuseIdentifier: "cell1")
        cell.textLabel?.text = model.title
        return cell
    case let .more(moreModel):
        let cell = UITableViewCell.init(style: .value1, reuseIdentifier: "cell2")
        cell.textLabel?.text = moreModel.description
        return cell
    }
})

测试数据

 let items = Observable.just([
        NewCellModel(header: "header1",items: [
                        .single(model: NewModel(id: 0, title: "1", content: "", hasMore: "")),
                        .single(model: NewModel(id: 1, title: "2", content: "", hasMore: "")),
                        .single(model: NewModel(id: 2, title: "3", content: "", hasMore: "")),
                        .more(model: NewMoreModel(id: 4, title: "4", content: "", description: "more1")),
                        .more(model: NewMoreModel(id: 4, title: "5", content: "", description: "more2")),
                        .more(model: NewMoreModel(id: 4, title: "6", content: "", description: "more3"))
        ]),
        NewCellModel(header: "header2",items: [
                        .single(model: NewModel(id: 0, title: "1", content: "", hasMore: "")),
                        .single(model: NewModel(id: 1, title: "2", content: "", hasMore: "")),
                        .single(model: NewModel(id: 2, title: "3", content: "", hasMore: "")),
                        .more(model: NewMoreModel(id: 4, title: "4", content: "", description: "more1")),
                        .more(model: NewMoreModel(id: 4, title: "5", content: "", description: "more2")),
                        .more(model: NewMoreModel(id: 4, title: "6", content: "", description: "more3"))
        ])
    ])

items.asDriver(onErrorJustReturn: [])
    .drive(tableView.rx.items(dataSource: dataSource))
    .disposed(by: disposeBag)

你可能感兴趣的:(RxDataSources 多数据源)