RxSwift 单元注册失败
RxSwift cell register failure
我使用 MVVM 架构,设置完所有内容后,TableView 中没有单元格,单元格文件中的断点也不会被命中。
在我的代码中:
控制器:
private lazy var tableView: BaseTableView = {
let tableView = BaseTableView()
tableView.register(MyCell.self, forCellReuseIdentifier: String(describing: MyCell.self))
tableView.rowHeight = 148
tableView.estimatedRowHeight = 148
self.view.addSubview(tableView)
tableView.snp.makeConstraints { (make) in
make.top.bottom.left.right.equalToSuperview()
}
return tableView
}()
let dataSource = RxTableViewSectionedReloadDataSource<MytSection>(configureCell: {(datasource, tableView, indexPath, model) -> UITableViewCell in
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MyCell.self), for: indexPath) as! MyCell
cell.model = model
return cell
})
viewModel.output
.items
.map { [MySection(header: "", items: [=10=])] }
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: bag)
单元文件中的断点没有被触发。我认为单元格未被调用,但我的委托和数据源都已正确绑定。
我想知道为什么。
注意下面的代码按预期工作。所以问题是,你在做什么不同?也许 snp.makeConstraints
并不像您认为的那样工作。也许您的 viewModel.output.items
没有发出任何后续事件。也许您的 MySection
实施不正确...
class MyViewController : UIViewController {
private lazy var tableView: UITableView = {
let tableView = UITableView(frame: view.bounds)
tableView.register(MyCell.self, forCellReuseIdentifier: String(describing: MyCell.self))
tableView.rowHeight = 148
tableView.estimatedRowHeight = 148
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(tableView)
return tableView
}()
let dataSource = RxTableViewSectionedReloadDataSource<MySection>(configureCell: {(datasource, tableView, indexPath, model) -> UITableViewCell in
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MyCell.self), for: indexPath) as! MyCell
cell.model = model
return cell
})
let bag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
Observable.just(["hello", "world"])
.map { [MySection(model: "", items: [=10=])] }
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: bag)
}
}
typealias MySection = SectionModel<String, String>
final class MyCell: UITableViewCell {
var model: String = "" {
didSet {
textLabel!.text = model
}
}
}
我使用 MVVM 架构,设置完所有内容后,TableView 中没有单元格,单元格文件中的断点也不会被命中。
在我的代码中: 控制器:
private lazy var tableView: BaseTableView = {
let tableView = BaseTableView()
tableView.register(MyCell.self, forCellReuseIdentifier: String(describing: MyCell.self))
tableView.rowHeight = 148
tableView.estimatedRowHeight = 148
self.view.addSubview(tableView)
tableView.snp.makeConstraints { (make) in
make.top.bottom.left.right.equalToSuperview()
}
return tableView
}()
let dataSource = RxTableViewSectionedReloadDataSource<MytSection>(configureCell: {(datasource, tableView, indexPath, model) -> UITableViewCell in
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MyCell.self), for: indexPath) as! MyCell
cell.model = model
return cell
})
viewModel.output
.items
.map { [MySection(header: "", items: [=10=])] }
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: bag)
单元文件中的断点没有被触发。我认为单元格未被调用,但我的委托和数据源都已正确绑定。
我想知道为什么。
注意下面的代码按预期工作。所以问题是,你在做什么不同?也许 snp.makeConstraints
并不像您认为的那样工作。也许您的 viewModel.output.items
没有发出任何后续事件。也许您的 MySection
实施不正确...
class MyViewController : UIViewController {
private lazy var tableView: UITableView = {
let tableView = UITableView(frame: view.bounds)
tableView.register(MyCell.self, forCellReuseIdentifier: String(describing: MyCell.self))
tableView.rowHeight = 148
tableView.estimatedRowHeight = 148
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(tableView)
return tableView
}()
let dataSource = RxTableViewSectionedReloadDataSource<MySection>(configureCell: {(datasource, tableView, indexPath, model) -> UITableViewCell in
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MyCell.self), for: indexPath) as! MyCell
cell.model = model
return cell
})
let bag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
Observable.just(["hello", "world"])
.map { [MySection(model: "", items: [=10=])] }
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: bag)
}
}
typealias MySection = SectionModel<String, String>
final class MyCell: UITableViewCell {
var model: String = "" {
didSet {
textLabel!.text = model
}
}
}