Swift 4:UITableView数据源无法正常工作

时间:2018-06-24 20:28:53

标签: ios swift uitableview

我正在尝试向UITableView对象添加一些数据。 我的问题是,在运行时不会调用数据源对象中的任何方法。 我已经检查了UITableView本身是否已显示。 当我调用providerTable.reloadData()时,仅调用numberOfSections(in tableView: UITableView) -> InttableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int方法,但从未调用过tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

import UIKit

class ProviderViewController: UIViewController {

    let navigationBar = { () -> UINavigationBar in
        let bar = UINavigationBar()
        bar.setItems([
            { () -> UINavigationItem in
                let item = UINavigationItem()
                item.title = "a title"
                return item
            }()
        ], animated: false)
        bar.translatesAutoresizingMaskIntoConstraints = false
        return bar
    }()
    let providerTable = { () -> UITableView in
        let providerDataSource = ProviderTableDataSource()

        let table = UITableView()
        table.dataSource = providerDataSource
        table.translatesAutoresizingMaskIntoConstraints = false
        table.register(ProviderTableViewCell.self, forCellReuseIdentifier: "providerCell")
        return table
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.setupWindow()
    }

    func setupWindow() -> Void {
        view.backgroundColor = .white

        view.addSubview(navigationBar)
        view.addSubview(providerTable)

        // Set constraints
        var constraints = [NSLayoutConstraint]()
        // navbar
        constraints.append(NSLayoutConstraint(item: navigationBar,
                                              attribute: .width,
                                              relatedBy: .equal,
                                              toItem: view,
                                              attribute: .width,
                                              multiplier: 1,
                                              constant: 0))
        constraints.append(NSLayoutConstraint(item: navigationBar,
                                              attribute: .left,
                                              relatedBy: .equal,
                                              toItem: view,
                                              attribute: .left,
                                              multiplier: 1,
                                              constant: 0))
        constraints.append(NSLayoutConstraint(item: navigationBar,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: view.safeAreaLayoutGuide,
                                              attribute: .top,
                                              multiplier: 1,
                                              constant: 0))
        // provider table
        constraints.append(NSLayoutConstraint(item: providerTable,
                                              attribute: .width,
                                              relatedBy: .equal,
                                              toItem: view,
                                              attribute: .width,
                                              multiplier: 1,
                                              constant: 0))
        constraints.append(NSLayoutConstraint(item: providerTable,
                                              attribute: .left,
                                              relatedBy: .equal,
                                              toItem: view,
                                              attribute: .left,
                                              multiplier: 1,
                                              constant: 0))
        constraints.append(NSLayoutConstraint(item: providerTable,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: navigationBar,
                                              attribute: .bottom,
                                              multiplier: 1,
                                              constant: 0))
        constraints.append(NSLayoutConstraint(item: providerTable,
                                              attribute: .bottom,
                                              relatedBy: .equal,
                                              toItem: view,
                                              attribute: .bottom,
                                              multiplier: 1,
                                              constant: 0))
        // activate constraints
        view.addConstraints(constraints)
    }

}

数据源对象:     导入UIKit

class ProviderTableDataSource: NSObject, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "providerCell", for: indexPath) as! ProviderTableViewCell
        cell.charterProvider = CharterProviders.providers[indexPath.row]

        return cell
    }
}

1 个答案:

答案 0 :(得分:6)

在创建表视图时,您没有强烈引用创建的ProviderTableDataSource

let providerTable = { () -> UITableView in
    let providerDataSource = ProviderTableDataSource()

    let table = UITableView()
    table.dataSource = providerDataSource
    table.translatesAutoresizingMaskIntoConstraints = false
    table.register(ProviderTableViewCell.self, forCellReuseIdentifier: "providerCell")
    return table
}()

UITableView的{​​{1}}属性很弱,因此您不能依靠它来强烈引用dataSource。在这里查看文档: https://developer.apple.com/documentation/uikit/uitableview/1614955-datasource?changes=_6

您想要做的可能是这样的:

class ProviderViewController: UIViewController {
    private let providerDataSource = ProviderTableDataSource()

    private(set) lazy var providerTable: UITableView = {
        let table = UITableView()
        table.dataSource = self.providerDataSource
        table.translatesAutoresizingMaskIntoConstraints = false
        table.register(ProviderTableViewCell.self, forCellReuseIdentifier: "providerCell")
        return table
    }()

    // [...]
}

这样,视图控制器就拥有对ProviderTableDataSource的强大引用,因此在创建表视图之后将不会释放该引用。