swi*_*dev 3 swift rx-swift rxdatasources
这是 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)
}
Run Code Online (Sandbox Code Playgroud)
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
}
}
Run Code Online (Sandbox Code Playgroud)
您的代码看起来不错,它可能是注释中建议的版本问题,但是您可以通过 像这样移动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
})
Run Code Online (Sandbox Code Playgroud)
其他一切都应该是一样的