ForEach 循环查看而不是图表数据

Posted

技术标签:

【中文标题】ForEach 循环查看而不是图表数据【英文标题】:ForEach Looping over View instead of Chart Data 【发布时间】:2020-06-12 14:09:45 【问题描述】:

我正在尝试获取ForEach 来填充数据。显然,这只是按预期创建了一个带有一个 dataPoint 的新视图。如何注入数组?

我在 ViewBuilder 之外尝试了 @Statefor-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)
            )
        
    

【问题讨论】:

【参考方案1】:

如果您希望您的ForEach 循环创建许多LineView 项目,您可以使用动态ForEach(带有显式id):

ForEach(scoreListVM.scoreCellViewModels, id:\.self)  scoreCellVM in
    LineView(data: [Double(scoreCellVM.score.totalScore)], title: "Acft Scores")

假设scoreListVM.scoreCellViewModels 变量是ScoreCellViewModel 项的数组。

【讨论】:

以上是关于ForEach 循环查看而不是图表数据的主要内容,如果未能解决你的问题,请参考以下文章

Foreach和doparallel而不是R中的for循环

在Django模板中使用for循环显示多个chart.js图表

如何限制与存储的数据库条目相关的@foreach 循环?

使用java 8中的forEach(..)而不是java 5中的forEach循环的任何优势[重复]

Visifire 图表反转循环顺序

foreach用法