我正在尝试将一些数据添加到UITableView对象. 我的问题是我的数据源对象中没有一个方法在运行时被调用. 我已经检查过UITableView本身是否显示. 当我调用providerTable.reloadData()时只调用num
我的问题是我的数据源对象中没有一个方法在运行时被调用.
我已经检查过UITableView本身是否显示.
当我调用providerTable.reloadData()时只调用numberOfSections(在tableView:UITableView中) – > Int和tableView(_ 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 } }您没有对创建表视图时创建的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的dataSource属性很弱,因此您不能依赖它来保存对ProviderTableDataSource的强引用.请参阅此处的文档:
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的强引用,因此在创建表视图后不会释放它.