【问题标题】:Implementing multiple sections with RxDatasources使用 RxDatasources 实现多个部分
【发布时间】:2022-01-19 18:42:39
【问题描述】:

我正在尝试使用 RxDatasources 制作多个部分(实际上是两个部分)。通常只有一个部分,我会这样:

截面模型:

import Foundation

import RxDataSources

typealias NotificationSectionModel = AnimatableSectionModel<String, NotificationCellModel>


struct NotificationCellModel : Equatable, IdentifiableType {
    
    static func == (lhs: NotificationCellModel, rhs: NotificationCellModel) -> Bool {
        
        return lhs.model.id == rhs.model.id
    }
    
    var identity: String {
        return model.id
    }
    var model: NotificationModel
    var cellIdentifier = "NotificationTableViewCell"
}

然后是实际模型:

struct NotificationModel: Codable, Equatable {
    let body: String
    let title:String
    let id:String
}

我会像这样使用它(在视图控制器中):

private func observeTableView(){
        let dataSource = RxTableViewSectionedAnimatedDataSource<NotificationSectionModel>(
            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()
            })
        
        notificationsViewModel.notifications
            .map{ notifications -> [NotificationCellModel] in
                return notifications.map{ NotificationCellModel( model: $0, cellIdentifier: NotificationTableViewCell.identifier) }
            }.map{ [NotificationSectionModel(model: "", items: $0)] }
            .bind(to: self.tableView.rx.items(dataSource: dataSource)).disposed(by: disposeBag)
    }

但是我将如何处理多个部分,使用不同类型的模型/单元格?

【问题讨论】:

  • 您上面显示的代码没有多大意义...例如它正在寻找NotificationModel 中的notification 属性,它显然不包含这样的属性...跨度>

标签: ios swift rx-swift rx-cocoa rxdatasources


【解决方案1】:

这是一种最坏的情况。您也许可以根据您的用例简化此代码:

// MARK: Model Code
struct ViewModel {
    let sections: Observable<[SectionModel]>
}

typealias SectionModel = AnimatableSectionModel<String, CellModel>

enum CellModel: IdentifiableType, Equatable {
    case typeA(TypeAInfo)
    case typeB(TypeBInfo)
    
    var identity: Int {
        switch self {
        case let .typeA(value):
            return value.identity
        case let .typeB(value):
            return value.identity
        }
    }
    
    var cellIdentifier: String {
        switch self {
        case .typeA:
            return "TypeA"
        case .typeB:
            return "TypeB"
        }
    }
}

struct TypeAInfo: IdentifiableType, Equatable {
    let identity: Int
}

struct TypeBInfo: IdentifiableType, Equatable {
    let identity: Int
}

// MARK: View Code
class Example: UIViewController {
    var tableView: UITableView!
    var viewModel: ViewModel!
    let disposeBag = DisposeBag()
    
    private func observeTableView(){
        let dataSource = RxTableViewSectionedAnimatedDataSource<SectionModel>(
            configureCell: { _, tableView, indexPath, item in
                guard let cell = tableView.dequeueReusableCell(withIdentifier: item.cellIdentifier, for: indexPath) as? BaseCell else { fatalError() }
                cell.setup(model: item)
                return cell
            })
        
        viewModel.sections
            .bind(to: tableView.rx.items(dataSource: dataSource))
            .disposed(by: disposeBag)
    }
}

class BaseCell: UITableViewCell {
    func setup(model: CellModel) { }
}
final class TypeACell: BaseCell { }
final class TypeBCell: BaseCell { }

【讨论】:

    猜你喜欢
    • 2019-08-18
    • 1970-01-01
    • 2021-05-17
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    相关资源
    最近更新 更多