Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxSwift access to indexPath.section

guys Im totally new in Rxswift, is there is a way to do this scenario in RxSwift ?

What I got is this.. but problem is i dont have 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)
            }

This is what I need in 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

datasource snippet:

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

Thanks for every help

like image 839
Denis Kakačka Avatar asked Feb 04 '23 10:02

Denis Kakačka


1 Answers

Here is an example from the repo to make a sectioned table view:

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

The upshot of it is that you have to instantiate a RxTableViewSectionedReloadDataSource.

However, I don't see in your code where you actually have sections. Sections in a UITableView imply a 2 dimensional array and you only have a 1 dimensional array...

like image 88
Daniel T. Avatar answered Feb 10 '23 23:02

Daniel T.