这是我的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 }
}
但是我仍然需要一个默认的返回值,我不知道它应该是什么。
我已经注册了xib,因为它确实已加载。现在他们只是显示不同的纯色背景
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")
}
答案 0 :(得分:0)
是的,您需要在所有返回值的方法中发送默认值,这里cellForRowAtIndexpath
返回UITableviewcelll
,因此必须提供默认值。
您可以使用的一个技巧是:
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 { // This will be your index 3
let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell4") as! TableViewCell4
return cell
}