3

这是 RxSwift 中的 tableView 我无法配置数据源。RxTableViewSectionedReloadDataSource 似乎缺少参数,尽管这很奇怪,因为我遵循与官方文档完全相同的代码源

Xcode 错误 每当我按回车键自动完成关闭时。关闭保持空白。

自动完成无效 我真的不知道如何解决这个问题

  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
    }
}
4

2 回答 2

6

您的代码看起来不错,这可能是评论中建议的版本问题,但是您可以通过像这样移动init中的配置来轻松解决它 :

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

其他一切都应该相同

于 2018-09-27T12:36:04.517 回答
0

将全局声明dataSource为可选变量:

var dataSource = RxTableViewSectionedReloadDataSource<SectionModel>?

我假设您正在viewDidLoad这样配置单元:

let dataSource = RxTableViewSectionedReloadDataSource<SectionModel>(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
            })

这是关键部分:

self.dataSource = dataSource
于 2019-01-28T21:43:16.443 回答