swift - 在一个 ViewController Swift 中使用两个 UITableView
问题描述
如何在一个 ViewController 中创建两个 UITableView,我有一个问题
您需要每个返回的问题都不在条件范围内,我有每个 Tableview 的信息
此消息:“预期返回'Int'的函数中缺少返回”
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == table_View {
return list.count
}
if tableView == table_View2 {
return list_2.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == table_View {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell_1") as! TableView_Cell
cell.la_view.text = list[indexPath.row]
cell.backgroundColor = UIColor(named: "Defeult")
return cell
}
if tableView == table_View2 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell_2") as! TableView_Cell
cell.la_view2.text = list_2[indexPath.row]
cell.backgroundColor = UIColor(named: "Defeult")
return cell
}
}
解决方案
问题是,例如在任何情况下numberOfRowsInSection
都必须返回一些东西。在您的实现中,您有两个if
语句,而您,但只有您知道这就足够了,因为tableView
只能是两者中的任何一个。不幸的是,编译器不知道这一点。因此,您可以通过一种简单的方式来做到这一点:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == table_View {
return list.count
}
return list_2.count
}
注意:同样适用于cellForRowAt
功能
可能更好:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == table_View {
return list.count
} else if tableView == table_View2 {
return list_2.count
}
assertionFailure("Unexpected tableView")
return 0
}
推荐阅读
- php - 尝试使用数据库中的内容填充引导药丸
- javascript - Ionic Angular - 动态图标选择
- swift - 不知道如何编写 func printStatsFor(number1 : Int, number2 : Int, number3 : Int) 的代码
- php - 如何排除某些路径出现在 Web Profiler 中?
- sql - Hive 表采样和分桶
- ubuntu - 我无法在 Ubuntu 中运行 Tensorboard
- symfony - 在最近版本的 faker 上坚持两次
- permissions - 如何使`getfacl`以特定顺序运行?
- reactjs - Material UI 嵌套主题提供程序与Styles HOC 中断
- php - 找不到图像字段