RxSwift 访问 indexPath.section

RxSwift access to indexPath.section

伙计们,我是 Rxswift 的新手,有没有办法在 RxSwift 中实现这个场景?

我得到的是这个..但问题是我没有 indexPath

datasource.sectionModels
        .asObservable()
        .bindTo(tableView.rx.items) { tableView, row, element in
            guard let sectionType = SectionType(rawValue: indexPath.section) else { return 0 }

            let indexPath = IndexPath(row: row, section: 0)

            var itemForIndexPath: SectionViewModel {
                return self.datasource.sectionModels.value[indexPath.section]
            }

            switch sectionType {
            case .nickTitle, .nickIfno:
                let infoCell = tableView.dequeueReusableCell(
                    withIdentifier: InfoTableViewCell.name,
                    for: indexPath
                    ) as! InfoTableViewCell

                var datasource: InfoCellDatasourceProtocol = InfoCellNormalState(text: itemForIndexPath.text)
                if itemForIndexPath.errorStyle {
                    datasource = InfoCellErrorState(text: itemForIndexPath.text)
                }

                infoCell.configureCell(datasource: datasource)
            }

这就是我在 RxSwift 中所需要的

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let sectionType = SectionType(rawValue: indexPath.section) else { return UITableViewCell() }

    var itemForIndexPath: SectionViewModel {
        return self.datasource.sectionModels.value[indexPath.section]
    }

    switch sectionType {
    case .nickTitle, .nickInfo:
        let infoCell = tableView.dequeueReusableCell(
            withIdentifier: InfoTableViewCell.name,
            for: indexPath
            ) as! InfoTableViewCell

        var datasource: InfoCellDatasourceProtocol = InfoCellNormalState(text: itemForIndexPath.text)
        if itemForIndexPath.errorStyle {
            datasource = InfoCellErrorState(text: itemForIndexPath.text)
        }

        infoCell.configureCell(datasource: datasource)

        return infoCell

数据源片段:

open class RegistrationNickDataSource: NickDatasourceProtocol {
    public var error: Variable<ErrorType>?
    public var success: Variable<Bool> = Variable(false)
    fileprivate let request = ValidateNameRequest()


    public var nickHints: Variable<[String]>?
    public var sectionModels: Variable<[SectionViewModel]> =  Variable([
    SectionViewModel(
        text: "your_nick_hint".localized,
        type: .info,
        errorStyle: false
    ),
    SectionViewModel(
        text: "your_nick_placeholder".localized,
        type: .input,
        errorStyle: false
    ),
    SectionViewModel(
        text: "your_nick_info".localized,
        type: .info,
        errorStyle: false
    )]
)

感谢大家的帮助

这里是 repo 中的一个示例,用于制作分段 table 视图:

https://github.com/ReactiveX/RxSwift/blob/master/RxExample/RxExample/Examples/SimpleTableViewExampleSectioned/SimpleTableViewExampleSectionedViewController.swift

结果是你必须实例化一个RxTableViewSectionedReloadDataSource

但是,我没有在您的代码中看到您实际拥有部分的位置。 UITableView 中的部分意味着一个二维数组,而你只有一个一维数组...

我建议使用 RxDataSources。当您调用 configureCell.

时,它会让您访问单元格的索引路径

您的数据源和单元格将使用如下内容配置:

func setupDataSource() 
{
    let dataSource = RxTableViewSectionedReloadDataSource<MySection>()

    dataSource.configureCell = { (theDataSource: TableViewSectionedDataSource<MySection>,                     
                                  theTableView,         
                                  theIndexPath,                                  
                                  item: MyItem) in
        let cell = theTableView.dequeueReusableCell(withIdentifier: InfoTableViewCell.name,
                                                    for: theIndexPath) as! InfoTableViewCell

        /* Do any setup of the cell here. */

        return cell;
    }       

    dataSource.titleForHeaderInSection = { theDataSource, index in
        return theDataSource.sectionModels[index].header;
    }
}

如果您在接受类型时遇到问题,您可以在闭包参数中省略类型。 Swift 可以为您推断。


为您的自定义数据源(包括您的部分模型)设置结构如下所示:

/// Holds data to display.
struct MyItem
{
    var text: String
    var type: MyCustomEnum
    var errorStyle: Bool
}

/// Defines a section type for use with sections for the table.
struct MySection
{
    var header: String
    var items: [Item]
}

/// Tie your custom data model to SectionModelType.
extension MySection: SectionModelType
{
    typealias Item = MyItem

    init(original: MySection,
         items: [Item])
    {
        self = original
        self.items = items
    }
}

倒数第二步是通过将您的部分放入 Observable 来绑定数据源。下面的代码是一个示例,其中函数 sections() returns 是一个 Observable<[MySection]> 类型的 Observable:

sections()
    .bind(to: tableView.rx.items(dataSource: dataSource))
    .disposed(by: bag)

最后,可以使用以下方法显示数据:

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