简体   繁体   English

使用 RxSwift 时未设置 Tableview 数据

[英]Tableview data isn't being set while using RxSwift

I am creating my tableview programmatically我正在以编程方式创建我的 tableview

extension UITableView {
static var tableView: UITableView {
     let tableView = UITableView()
     tableView.register(UITableViewCell.self, forCellReuseIdentifier: "menuCell")
     tableView.translatesAutoresizingMaskIntoConstraints = false
     return tableView
    }
}

I am calling a binding function during viewdidload我在 viewdidload 期间调用绑定函数

private func setUpBindings() {
    guard let viewModel = self.viewModel else { return }

    viewModel.menuItems
        .bind(to: tableView.rx.items(cellIdentifier: "menuCell", cellType: UITableViewCell.self)) { [weak self] row, model, cell in
            Logger.info("Called")
            cell.selectionStyle = .none
            cell.textLabel?.text = model.uppercased()
            cell.textLabel?.textColor = self?.selectedRow == row ? .white : .darkGray
            cell.backgroundColor = self?.selectedRow == row
                ? ColorPreference.mainColor
                : UIColor.clear
        }
        .disposed(by: self.disposeBag)
        self.tableView.reloadData()
}

The datasource for my tableview is from an observable in my viewmodel我的 tableview 的数据源来自我的 viewmodel 中的一个 observable

let menuItems = Observable.just([
    "Recommended",
    "Dashboard",
    "Settings"
])

The problem I'm having is that the bind function isn't being called for the tableview .我遇到的问题是没有为tableview调用 bind 函数。 There are clearly items in the observable that should be bound but every possible test I've done has never executed the body of the tableview binding. observable 中显然有一些项目应该被绑定,但我所做的每一个可能的测试都从未执行过 tableview 绑定的主体。

To confirm my theory, I tested this code within setupBindings() and the body of it was executed, printing out "2019-12-01 16:36:18 | INFO | ["Recommended", "Dashboard", "Settings"]"为了证实我的理论,我在 setupBindings() 中测试了这段代码并执行了它的主体,打印出 "2019-12-01 16:36:18 | INFO | ["Recommended", "Dashboard", "Settings"] “

viewModel.menuItems
        .bind(onNext: { items in
            Logger.info("\(items)")
        })
        .disposed(by: self.disposeBag)

I've looked at the documentation for properly settings tableview's datasource and have done my fair share of research.我查看了有关正确设置 tableview 数据源的文档,并进行了相当多的研究。 I'm stumped and need help.我很难过,需要帮助。

Really silly error.真是愚蠢的错误。 Before I fixed it, my tableview constraints looked like this在我修复它之前,我的 tableview 约束看起来像这样

self.tableView.topAnchor.constraint(equalTo: appLabel.bottomAnchor, constant: 30).isActive = true
self.tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
self.tableView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true

I forgot to set the bottom constraint of the tableview to its superview.我忘了将表视图的底部约束设置为其超级视图。 From what I could extrapolate, this prevented the tableview from being shown in the view.据我推断,这阻止了 tableview 在视图中显示。 Without it being shown, it isn't possible to set the data source of the tableview, as pointed out by Arvin in the comments.如果不显示,就不可能设置表视图的数据源,正如 Arvin 在评论中指出的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM