RxSwift 访问indexPath.section

den*_*cka 5 uitableview ios swift rx-swift indexpath

伙计们,我对 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)
            }
Run Code Online (Sandbox Code Playgroud)

这就是我在 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
Run Code Online (Sandbox Code Playgroud)

数据源片段:

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
    )]
)
Run Code Online (Sandbox Code Playgroud)

感谢您的每一次帮助

Dan*_* T. 5

以下是存储库中用于制作分区表视图的示例:

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

其结果是您必须实例化一个RxTableViewSectionedReloadDataSource.

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


Dan*_*ang 4

我建议使用RxDataSources。当您调用 时,它将使您能够访问单元格的索引路径configureCell

\n\n

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

\n\n
func setupDataSource() \n{\n    let dataSource = RxTableViewSectionedReloadDataSource<MySection>()\n\n    dataSource.configureCell = { (theDataSource: TableViewSectionedDataSource<MySection>,                     \n                                  theTableView,         \n                              \xc2\xa0 \xc2\xa0 theIndexPath,                                  \n                              \xc2\xa0 \xc2\xa0 item: MyItem) in\n        let cell = theTableView.dequeueReusableCell(withIdentifier: InfoTableViewCell.name,\n                                                    for: theIndexPath) as! InfoTableViewCell\n\n        /* Do any setup of the cell here. */\n\n        return cell;\n    }       \n\n    dataSource.titleForHeaderInSection = { theDataSource, index in\n        return theDataSource.sectionModels[index].header;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果您在获取类型时遇到问题,可以忽略闭包参数中的类型。Swift 可以为您推断它们。

\n\n
\n\n

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

\n\n
/// Holds data to display.\nstruct MyItem\n{\n    var text: String\n    var type: MyCustomEnum\n    var errorStyle: Bool\n}\n\n/// Defines a section type for use with sections for the table.\nstruct MySection\n{\n    var header: String\n    var items: [Item]\n}\n\n/// Tie your custom data model to SectionModelType.\nextension MySection: SectionModelType\n{\n    typealias Item = MyItem\n\n    init(original: MySection,\n         items: [Item])\n    {\n        self = original\n        self.items = items\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

倒数第二步是通过将您的部分放入 Observable 中来绑定数据源。sections()以下代码是函数返回类型为 Observable 的示例Observable<[MySection]>

\n\n
sections()\n    .bind(to: tableView.rx.items(dataSource: dataSource))\n    .disposed(by: bag)\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

最后,可以使用以下命令来显示数据:

\n\n
 override func viewDidLoad()\n {\n     super.viewDidLoad()\n     setupDataSource()\n }\n
Run Code Online (Sandbox Code Playgroud)\n