【发布时间】:2018-08-12 21:20:48
【问题描述】:
这是我的 cellForRowAtIndexPath
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell1") as! TableViewCell1
return cell
} else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell2") as! TableViewCell2
return cell
} else if indexPath.row == 2{
let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell3") as! TableViewCell3
return cell
} else if indexPath.row == 3 {
let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell4") as! TableViewCell4
return cell }
}
但我仍然需要一个默认返回值,我不知道它应该是什么。
我已经在视图中注册了 xibs 确实加载了。现在他们只是显示不同的纯色背景
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.register(UINib(nibName: "TableViewCell1", bundle: nil), forCellReuseIdentifier: "tablecell1")
self.tableView.register(UINib(nibName: "TableViewCell2", bundle: nil), forCellReuseIdentifier: "tablecell2")
self.tableView.register(UINib(nibName: "TableViewCell3", bundle: nil), forCellReuseIdentifier: "tablecell3")
self.tableView.register(UINib(nibName: "TableViewCell4", bundle: nil), forCellReuseIdentifier: "tablecell4")
}
【问题讨论】:
-
return UITableViewCell()? -
我做错了,tableView 是空的。它应该只显示 4 行不同颜色的单元格。
-
UITableView将始终生成足够的单元格以覆盖整个屏幕(或至少覆盖其高度)