【问题标题】:RxSwift access to indexPath.sectionRxSwift 访问 indexPath.section
【发布时间】:2017-05-18 09:16:22
【问题描述】:

伙计们,我是 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

数据源sn-p:

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

感谢大家的帮助

【问题讨论】:

    标签: ios swift uitableview rx-swift indexpath


    【解决方案1】:

    这是一个来自 repo 的示例,用于制作分段表视图:

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

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

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

    【讨论】:

      【解决方案2】:

      我建议使用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()返回Observable&lt;[MySection]&gt;类型的Observable的示例:

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

      最后,让数据出现可以使用:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-10
        • 2017-10-03
        • 1970-01-01
        • 2016-02-26
        相关资源
        最近更新 更多