我有一个带按钮的 Controller 和一个带 10 个数组的 TableViewController
。每个按钮都有一个索引,该索引传递给 TableViewController
。
在 TableViewController
中,代码如下所示:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if buttonIndex == 0 {
return array0.count
}
else if buttonIndex == 1 {
return array1.count
}
else if buttonIndex == 2 {
return array2.count
}
else if buttonIndex == 3 {
return array3.count
}
else if buttonIndex == 4 {
return array4.count
}
else if buttonIndex == 5 {
return array5.count
}
else if buttonIndex == 6 {
return array6.count
}
else if buttonIndex == 7 {
return array6.count
}
else if buttonIndex == 8 {
return array6.count
}
return array0.count
}
我想自动定义当前索引来执行此操作:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return array(currentIndex).count
}
怎么做?
最佳答案
实现您想要的目标的一种方法是创建一个映射,其中索引作为键,数组作为值。
所以你的代码应该是这样的:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return indicesArray[index].count
}
关于ios - TableViewController numberOfRowsInSection 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45752164/