我正在尝试获取ForEach
来填充数据。显然,这只是按预期创建了一个只有一个dataPoint的新视图。如何注入数组?
我在ViewBuilder之外尝试了@State
和一个for-in
循环,但是由于某些原因而无法正常工作。
struct DashboardView: View {
@ObservedObject var scoreListVM = ScoreListViewModel()
@State private var isPresented = false
var body: some View {
NavigationView {
VStack {
ForEach(scoreListVM.scoreCellViewModels) { scoreListVM in
LineView(data: [Double(scoreListVM.score.totalScore)], title: "Acft Scores")
}
}
.navigationBarTitle("Dashboard")
.navigationBarItems(trailing:
Button(action: {
self.isPresented.toggle()
}) {
Image(systemName: "book.circle")
.imageScale(.medium)
}
.sheet(isPresented: $isPresented) {
InstructionView()
.environmentObject(self.colorSchemes)
})
}
}
}
答案 0 :(得分:0)
如果您希望ForEach
循环创建许多LineView
项目,则可以使用动态ForEach
(带有显式id
):
ForEach(scoreListVM.scoreCellViewModels, id:\.self) { scoreCellVM in
LineView(data: [Double(scoreCellVM.score.totalScore)], title: "Acft Scores")
}
假设scoreListVM.scoreCellViewModels
变量是ScoreCellViewModel
个项目的数组。