具有自定义数据源的 UITableViewController 不显示单元格
UITableViewController with custom data source not showing cells
我目前有一个 UITableViewController,它在其初始化函数中设置了自定义数据源:
class BookmarkTableViewController: UITableViewController {
var date: Date
// MARK: - Init
init(date: Date, fetchAtLoad: Bool) {
self.date = date
super.init(style: .plain)
self.tableView.dataSource = BookmarkDataSource(date: date)
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
// ...
}
自定义数据源如下:
class BookmarkDataSource: NSObject {
let date: Date
init(date: Date) {
self.date = date
super.init()
}
}
// MARK: - UITableViewDataSource
extension BookmarkDataSource: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "Test content"
return cell
}
}
然而,当我在模拟器或设备上 运行 时,table 视图中没有显示任何内容。有谁知道我错过了什么?
注意:我使用的是 Xcode 8.0 Beta 和 Swift 3.
我认为您的单元格未正确实例化。
尝试替换
let cell = UITableViewCell()
和
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
您需要存储对 BookmarkDataSource 对象的强引用。 tableView 的 dataSource 变成了你发布的代码。
class BookmarkTableViewController: UITableViewController {
var date: Date
var dataSource:BookmarkDataSource
// MARK: - Init
init(date: Date, fetchAtLoad: Bool) {
self.date = date
super.init(style: .plain)
dataSource = BookmarkDataSource(date: date)
self.tableView.dataSource = dataSource
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
// ...
}
我目前有一个 UITableViewController,它在其初始化函数中设置了自定义数据源:
class BookmarkTableViewController: UITableViewController {
var date: Date
// MARK: - Init
init(date: Date, fetchAtLoad: Bool) {
self.date = date
super.init(style: .plain)
self.tableView.dataSource = BookmarkDataSource(date: date)
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
// ...
}
自定义数据源如下:
class BookmarkDataSource: NSObject {
let date: Date
init(date: Date) {
self.date = date
super.init()
}
}
// MARK: - UITableViewDataSource
extension BookmarkDataSource: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "Test content"
return cell
}
}
然而,当我在模拟器或设备上 运行 时,table 视图中没有显示任何内容。有谁知道我错过了什么?
注意:我使用的是 Xcode 8.0 Beta 和 Swift 3.
我认为您的单元格未正确实例化。
尝试替换
let cell = UITableViewCell()
和
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
您需要存储对 BookmarkDataSource 对象的强引用。 tableView 的 dataSource 变成了你发布的代码。
class BookmarkTableViewController: UITableViewController {
var date: Date
var dataSource:BookmarkDataSource
// MARK: - Init
init(date: Date, fetchAtLoad: Bool) {
self.date = date
super.init(style: .plain)
dataSource = BookmarkDataSource(date: date)
self.tableView.dataSource = dataSource
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
// ...
}