Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxTableViewSectionedReloadDataSource

This is a tableView in RxSwift I am not able to configure the dataSource. There seems to be parameters missing for RxTableViewSectionedReloadDataSource, although this is strange as I follow the exact same code source of the official docs

Xcode error Whenever I hit enter to autocomplete the closure. The closure remains blank.

autocomplet not effectiv I really don't know how to resolve this one

  let dataSource = RxTableViewSectionedReloadDataSource<SectionModel>() 



dataSource?.configureCell = { (ds: RxTableViewSectionedReloadDataSource<SectionOfCustomData>, tv: UITableView, ip: IndexPath, item: Article) -> NewsFeedCell in
                let cell = tv.dequeueReusableCell(withIdentifier: "Cell", for: ip) as! NewsFeedCell
                cell.configure(news: item)
                return cell
            }
            dataSource?.titleForHeaderInSection = { ds, index in
                return ds.sectionModels[index].header
            }

    let sections = [
        SectionOfCustomData(header: "First section", items: self.articles),
        SectionOfCustomData(header: "Second section", items: self.articles)
        ]

            guard let dtSource = dataSource else {
                return
            }
            Observable.just(sections)
                .bind(to: tableView.rx.items(dataSource: dtSource))
                .disposed(by: bag)

        }

SectionModel.swift

import Foundation
import RxSwift
import RxCocoa
import RxDataSources

struct SectionOfCustomData {
    var header: String
    var items: [Item]
}
extension SectionOfCustomData: SectionModelType {
    typealias Item = Article

    init(original: SectionOfCustomData, items: [Item]) {
        self = original
        self.items = items
    }
}
like image 558
swiftdev Avatar asked Feb 18 '26 18:02

swiftdev


1 Answers

Your code seems fine, it might be a version problem as suggested in the comment, but you can easily solve it by moving the configurations in init like this:

let dataSource = RxTableViewSectionedReloadDataSource<SectionModel>(
        configureCell: { (dataSource, tableView, indexPath, item) -> NewsFeedCell in
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! NewsFeedCell
            cell.configure(news: item)
            return cell
        },
        titleForHeaderInSection: { dataSource, index in
            return dataSource.sectionModels[index].header
        })

Everything else should be the same

like image 137
boa_in_samoa Avatar answered Feb 20 '26 17:02

boa_in_samoa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!