Vla*_*lad 2 uitableview ios swift
请帮我用字典中的数据填充表格视图单元格。例如,我有这样的单元格:
为了用数据填充它,我开始使用覆盖cellForRowAt方法:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CurrencyCell", for: indexPath) as! CurrencyCell
for (key, value) in currencies! {
print("key is - \(key) and value is - \(value)")
}
// ?
// cell.currencyLabel.text =
// cell.askLabel.text =
// cell.bidLabel.text =
return cell
}
Run Code Online (Sandbox Code Playgroud)
在这里打印字典:
key is - EUR and value is - Rate(ask: Optional("30.8500"), bid: Optional("30.1000"))
key is - USD and value is - Rate(ask: Optional("26.3000"), bid: Optional("26.0500"))
key is - RUB and value is - Rate(ask: Optional("0.4150"), bid: Optional("0.3750"))
Run Code Online (Sandbox Code Playgroud)
这该怎么做?提前致谢!
我使用 struct Rate 来重现您当前的输出
struct Rate {
var ask : Float?
var bid : Float?
static var shared = Rate()
mutating func initWithDictValues(_ currentRate : Rate) {
self.ask = currentRate.ask
self.bid = currentRate.bid
}
}
Run Code Online (Sandbox Code Playgroud)
货币数组
/// Array Declaration
var currencies = [String:Any]()
/// Add Values
currencies = ["EUR":Rate(ask: 30.8500, bid: 30.8500),"USD":Rate(ask: 26.3000, bid: 26.3000),"RUB":Rate(ask: 0.4150, bid: 0.4150)]
Run Code Online (Sandbox Code Playgroud)
获取单独数组中的所有键,以便我们可以轻松地使单元出列
var keysArray = Array(currencies.keys)
Run Code Online (Sandbox Code Playgroud)
表视图函数
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CurrencyCell", for: indexPath) as! CurrencyCell
/// Get CurrentKey
let currentKey = keysArray[indexPath.row]
let currentIndexKey : Rate = currencies[currentKey] as! Rate
/// Assign Values
cell.currencyLabel.text = currentKey
cell.askLabel.text = currentIndexKey.ask ?? 0
cell.bidLabel.text = currentIndexKey.bid ?? 0
return cell
}
Run Code Online (Sandbox Code Playgroud)
游乐场输出
希望这可以帮助
| 归档时间: |
|
| 查看次数: |
5335 次 |
| 最近记录: |