oiv*_*111 3 uitableview ios swift
我正在尝试将一些数据添加到UITableView对象.我的问题是我的数据源对象中没有一个方法在运行时被调用.我已经检查过UITableView本身是否显示.当我打电话providerTable.reloadData()只是numberOfSections(in 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)
}
}
Run Code Online (Sandbox Code Playgroud)
数据源对象:import 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
}
}
Run Code Online (Sandbox Code Playgroud)
您没有对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因此在创建表视图后不会释放它.
| 归档时间: |
|
| 查看次数: |
182 次 |
| 最近记录: |