【发布时间】:2021-01-26 20:49:54
【问题描述】:
我是 iOS 开发新手。
我正在尝试使用 UITableView 在我的应用中显示项目列表,但项目未显示:
我的视图控制器:
class HistoryView: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let cellReuseIdentifier = "cell"
@IBOutlet weak var noDataLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(HistoryTableViewCell.self, forCellReuseIdentifier: "historyCell")
tableView.delegate = self
tableView.dataSource = self
self.noDataLabel.isHidden=true
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "historyCell") as! HistoryTableViewCell
cell.nickname?.text = "name"
return cell
}
}
我的表格视图单元格:
class HistoryTableViewCell: UITableViewCell {
@IBOutlet weak var nickname: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
对发生的事情有什么建议吗?
【问题讨论】:
-
numberOfRowsInSection被调用了吗? (检查print或断点。) -
检查你的tableview没有隐藏
-
您使用的是原型单元格吗?如果是,则您不得注册该单元。
-
表格视图未隐藏
-
是 numberOfRowsInSection 调用了 10 次
标签: ios swift uitableview tableview