gpt4 book ai didi

ios - 根据另一个数组的索引检索值

转载 作者:行者123 更新时间:2023-11-28 11:55:56 24 4
gpt4 key购买 nike

长期以来,我一直面临着这方面的问题。

  • 问题:

我需要根据我的 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 的 cellForRowAt indexPath 中,我需要在 indexPath 0 处设置“MD”值(需要根据 indexArray 的索引值)

通过这种方式,对于我的 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
}

帮助将不胜感激,谢谢! :)

最佳答案

首先,您需要使用 cellForRowAt: 方法中的索引路径从 indexArray 获取索引。然后使用该索引(从 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
}

关于ios - 根据另一个数组的索引检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50988239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com