2017-03-09 9 views
0

TableViewでRxSwift/RxDataSourceを使用しようとしていますが、configureCellに既存の関数を割り当てることができません。以下のコード:RxSwift DataSource 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のは、次のエラーがスローされます。エラーがライン

dataSource.configureCell = cell

でスローされ

/Users/.../BaseTableViewController.swift:39:36: Cannot assign value of type '(TableViewSectionedDataSource, UITableView, IndexPath, TableSectionModel.Item) -> UITableViewCell!' to type '(TableViewSectionedDataSource, UITableView, IndexPath, TableSectionModel.Item) -> UITableViewCell!'

あなたはどんな考えを持っていますか?

おかげ

+0

プットのdataSource ViewModelのセクションオブジェクト – denis631

答えて

0

は、あなたは自分のセルメソッドの戻り値の型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 
} 
関連する問題