gpt4 book ai didi

Swift tableView cellForRowAt 获取数组的 subKeyName 的数组

转载 作者:行者123 更新时间:2023-11-30 10:39:33 25 4
gpt4 key购买 nike

我有这样的数组数据

var countryData = [Country(code: "01", name: "US", currency: "USD", information: [Information(first: "100", second: "200", third: "300"), information(first: "400", second: "500", third: "600"), information(first: "700", second: "800", third: "900")])]

我需要使用tableView来显示这些数据

在 cellForRowAt 中我可以获得国家/地区的值

像这样

cell.countryNameLabel.text = countryData[indexPath.row].name

但是如果有 subKay 并且它是数组怎么办

我应该如何获取该数组的数据并显示全部?

cell.countryInformationLabel.text = ?

我的手机看起来像

US : 100 200 300, 400 500 600,700 800 900

最佳答案

你可以做到

let information = countryData[indexPath.row].information
cell.countryInformationLabel.text = information
.map { String(describing: $0) }
.joined(separator: ", ")

extension Information: CustomStringConvertible {
var decription: String {
return "\(first) \(second) \(third)"
}
}

如果您认为没有必要遵守CustomStringConvertible,只需执行以下操作:

let information = countryData[indexPath.row].information
cell.countryInformationLabel.text = information
.map { "\($0.first) \($0.second) \($0.third)" }
.joined(separator: ", ")

结果如下:

struct Information {
var first: String
var second: String
var third: String
}

let information = [Information(first: "100", second: "200", third: "300"),
Information(first: "300", second: "400", third: "500"),
Information(first: "400", second: "500", third: "600")]
print(
information
.map { "\($0.first) \($0.second) \($0.third)" }
.joined(separator: ", ")
)
// 100 200 300, 300 400 500, 400 500 600

关于Swift tableView cellForRowAt 获取数组的 subKeyName 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57156355/

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