4

伙计们,我在 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
    )]
)

感谢您的每一个帮助

4

2 回答 2

4

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

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

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

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

于 2017-05-18T13:49:10.700 回答
4

我建议使用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 的示例Observable<[MySection]>

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

最后,可以使用以下方法获取数据:

 override func viewDidLoad()
 {
     super.viewDidLoad()
     setupDataSource()
 }
于 2017-05-23T01:39:02.097 回答