注册的 tableViewCell 不显示文本标签
Registered tableViewCell doesn't show the text label
我创建了一个 tableView,其中注册的 Cell 的 reuseIdentifier 为“cell”。我已将此 tableView 作为子视图添加到 superview。
private let tableView: UITableView = {
let table = UITableView()
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return table
}()
我已经对 TableViewCellDelegate 和 DataSource 的协议函数进行了如下安慰。但是当我执行 运行 应用程序时,它仍然只显示我的 tableView 而不是应该写“Hello”的单元格。为什么会发生这种情况?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return notifications.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = notifications[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Hello"
return cell
}
请检查您的通知数组是否为空,如果您从 api 获取它,请确保调用 tableView.reloadData()
您是否已将 tableView 添加到视图中?
在 ViewController 中添加下面的代码,它应该可以工作
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
我创建了一个 tableView,其中注册的 Cell 的 reuseIdentifier 为“cell”。我已将此 tableView 作为子视图添加到 superview。
private let tableView: UITableView = {
let table = UITableView()
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return table
}()
我已经对 TableViewCellDelegate 和 DataSource 的协议函数进行了如下安慰。但是当我执行 运行 应用程序时,它仍然只显示我的 tableView 而不是应该写“Hello”的单元格。为什么会发生这种情况?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return notifications.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = notifications[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Hello"
return cell
}
请检查您的通知数组是否为空,如果您从 api 获取它,请确保调用 tableView.reloadData()
您是否已将 tableView 添加到视图中?
在 ViewController 中添加下面的代码,它应该可以工作
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}