簡體   English   中英

使用 RxDatasources 實現多個部分

[英]Implementing multiple sections with RxDatasources

我正在嘗試使用 RxDatasources 制作多個部分(實際上是兩個部分)。 通常有一個部分,我會像這樣 go :

model 部分:

import Foundation

import RxDataSources

typealias NotificationSectionModel = AnimatableSectionModel<String, NotificationCellModel>


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

然后是實際的 model:

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)
    }

但是,我如何將 go 與多個部分,不同類型的模型/單元?

這是一種最壞的情況。 您可能可以根據您的用例簡化此代碼:

// 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 { }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM