【问题标题】:RxSwift DataSource configureCell not able to assign functionRxSwift DataSource configureCell 无法分配功能
【发布时间】:2017-03-09 07:22:34
【问题描述】:

我正在尝试将 RxSwift/RxDataSource 与 TableView 一起使用,但我无法为 configureCell 分配现有函数。代码如下:

import UIKit
import RxSwift
import RxCocoa
import RxDataSources

class BaseTableViewController: UIViewController {
    // datasources
    let dataSource = RxTableViewSectionedReloadDataSource<TableSectionModel>()
    let sections: Variable<[TableSectionModel]> = Variable<[TableSectionModel]>([])
    let disposeBag: DisposeBag = DisposeBag()

    // components
    let tableView: UITableView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        setDataSource()
    }

    func setupUI() {
        attachViews()
    }

    func setDataSource() {
        tableView.delegate = nil
        tableView.dataSource = nil
        sections.asObservable()
            .bindTo(tableView.rx.items(dataSource: dataSource))
            .addDisposableTo(disposeBag)
        dataSource.configureCell = cell
        sectionHeader()
    }

    func cell(ds: TableViewSectionedDataSource<TableSectionModel>, tableView: UITableView, indexPath: IndexPath, item: TableSectionModel.Item) -> UITableViewCell! {
        return UITableViewCell()
    }

    func sectionHeader() {

    }
}

Xcode 抛出以下错误:

/Users/.../BaseTableViewController.swift:39:36:无法分配类型“(TableViewSectionedDataSource,UITableView,IndexPath,TableSectionModel.Item)-> UITableViewCell!”的值输入'(TableViewSectionedDataSource, UITableView, IndexPath, TableSectionModel.Item) -> UITableViewCell!'

错误在行抛出

dataSource.configureCell = 单元格

你有什么想法吗?

谢谢

【问题讨论】:

  • 将 dataSource 和 section 对象放入 ViewModel :)

标签: ios swift uitableview rx-swift rxdatasources


【解决方案1】:

您只需从单元方法的返回类型UITableViewCell! 中删除!

func cell(ds: TableViewSectionedDataSource<TableSectionModel>, tableView: UITableView, indexPath: IndexPath, item: TableSectionModel.Item) -> UITableViewCell {
    return UITableViewCell()
}

通过这种方式,你的函数变得与 RxDataSource 的 configureCell 属性所期望的类型兼容:

public typealias CellFactory = (TableViewSectionedDataSource<S>, UITableView, IndexPath, I) -> UITableViewCell

我个人更喜欢以下语法来初始化configureCell

dataSource.configureCell = { (_, tableView, indexPath, item) in
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    // Your configuration code goes here
    return cell
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多