很长一段时间以来,我一直面临这个问题。
我需要根据tableView中以下数组的索引获取值。
indexArray = [3, 6, 4, 5, 8, 30, 31, 7, 18, 29, 1]
paramNameArray = ["Date", "Sync Time", "UUC2T7", "Set MD", "Total KVA", "Total KW", "Running MD", "SMS 1 KW", "P.F.", "ET-5 KW", "ET-3_4 KW", "ET- 6_7 KW", "ABP-1 KW", "ABP-1A KW", "ABP-2 KW", "G-22 KW", "UUC36T37", "G-23 KW", "WRM KW", "Total KWH", "Total KVAH", "ET-5 KWH", "ET-3_4 KWH", "ET-6_7 KWH", "ABP-1 KWH", "ABP-1A KWH", "ABP-2 KWH", "SMS 1 KWH", "UUC60T73", "PLC Time", "Voltage", "Current", "Load Factor", "Avg. P.F."]
问题:
这样,对于tableView的每一行,我都需要按照indexArray的索引值。
例如:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
(at 0th Index) cell.lblParameter.text = "Set MD" // value from paramNameArray as per indexArray's index(3)
(at 1st Index) cell.lblParameter.text = "Running MD" // value from paramNameArray as per indexArray's index(6)
// And so on...
return cell
}
非常感谢您的帮助,谢谢! :)
答案 0 :(得分:2)
首先,您需要使用indexArray
方法中的索引路径从cellForRowAt:
获取索引。然后使用该索引(从indexArray
获得)从paramNameArray
获得价值。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let index = indexArray[indexPath.row] as! Int
//check that index won't exceeds `paramNameArray.count` otherwise it will throw `ArrayIndexOutOfBoundsException`.
if index < paramNameArray.count {
let text = paramNameArray[index]
cell.lblParameter.text = text
} else {
cell.lblParameter.text = "some default text goes here in case if value is not present"
}
return cell
}