【发布时间】:2021-11-06 02:28:13
【问题描述】:
我正在从服务器获取数据,我需要像在此站点中显示的那样显示它 SwiftUI - nested list
来自服务器的数据是
"OptionsListByItemId": [
{
"Choices": [
{
"ChoiceId": 1284226,
"ChoiceName": "Hot",
},
{
"ChoiceId": 1284227,
"ChoiceName": "Cool",
}
],
"OptionId": 187472,
"OptionName": "Temperature"
},
{
"Choices": [
{
"ChoiceId": 1284223,
"ChoiceName": "61%",
},
{
"ChoiceId": 1284224,
"ChoiceName": "70%",
}
],
"OptionId": 187473,
"OptionName": "Humidity"
]
}
我的模型是这样的
struct OptionsandChoices : Decodable , Identifiable{
var id: String{OptionName}
var OptionName: String!
var OptionId: Int
var Choices : [ChoiseList]
}
struct OptionsandChoiceList: Decodable{
var OptionsListByItemId:[OptionsandChoices]
}
struct ChoiseList: Decodable {
var ChoiceName: String!
var ChoiceId: Int
}
ViewModel 是
class ItemChoiceViewModel : ObservableObject{
@Published var OpnChoice: OptionsandChoiceList = OptionsandChoiceList(OptionsListByItemId: [])
// fetching data from server
}
我的 swiftUI 视图喜欢
struct ItemView: View {
var OpnChoice = OptionsandChoiceList(OptionsListByItemId: [])
@ObservedObject var choicevwModel = ChoiceViewModel()
struct Optionspage: View {
var body: some View {
List(choicevwModel.OpnChoice.OptionsListByItemId) {opn in
Text(opn.OptionName)
}
}
我无法在列表中使用 ChoiceName
我怎样才能在 OptionName 下面的每一行中获得choiceName,就像我给出的链接中一样
列表应该显示为
Temperature
Hot
Cold
Humidity
61%
70%
目前我排成两排
Temperature
Humidity
【问题讨论】:
-
遍历
Choices -
@loremipsum 如果我给 Text(opn.Choices.ChoiceName) 它会给我错误“'[ChoiseList]' 类型的值没有成员 'ChoiceName'”
-
迭代,使用某种循环
标签: ios iphone xcode ipad swiftui