swift - ForEach 循环查看而不是图表数据
问题描述
我正在尝试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)
})
}
}
}
解决方案
如果您希望ForEach
循环创建许多LineView
项目,则可以使用动态ForEach
(带有显式id
):
ForEach(scoreListVM.scoreCellViewModels, id:\.self) { scoreCellVM in
LineView(data: [Double(scoreCellVM.score.totalScore)], title: "Acft Scores")
}
假设scoreListVM.scoreCellViewModels
变量是一个ScoreCellViewModel
项目数组。
推荐阅读
- javascript - 如何在不阻塞 onload 或等待 onload 的情况下加载 iframe
- django - 模板中的Django select_related查询集未呈现
- python - 我在 heroku 上部署了一个烧瓶应用程序,它立即崩溃了
- jquery - 如何使用 JQuery 从 JSON 对象数组中获取值
- python - pd.read_html bs4 导入成功后找不到
- android - 主 UI 线程和 AsyncTask 之间的同步问题
- javascript - onClick 跳转到页面顶部
- elixir - 将 args 从自定义插件转发到第二个插件
- c# - WebApi .Net Core 错误,因为不包含 GetAwaiter 的定义
- c - 如何找到我们用指针保留的空间?