【问题标题】:SwiftUI get value of next loop in For EachSwiftUI 在 For Each 中获取下一个循环的值
【发布时间】:2020-04-21 19:42:19
【问题描述】:

我正在尝试显示在日期('date')上输入的值('timeMinutes' 的总和)。我将在 ForEach 循环中显示所有这些条目。这个想法是我计算新的 timeMinutes 是高于还是低于上次输入的时间。

请注意,日期和时间是一对多的关系。

制作 ForEach 循环很好:

    ForEach(dates, id: \.self) { day in
        Section(header: Text("\(self.formatDate(date: day.date!))")) {
            ForEach(day.timeArray, id: \.self) { time in
                VStack {
                Text("\(String(time.timeMinutes))")
                }
            }
            Text("Total time is \(self.time.filter{$0.date.date == day.date}.map{$0.timeMinutes}.reduce(0, +))")
            Text("The last time you did this was: \(DATA FROM NEXT LOOP????)")
            Text("The difference is \(ADD/SUBSTRACT - RESULT)")
        }
    }

基本上我想要下一个循环的“总时间”。我该怎么做呢?我的日期是按日期排序的,所以转到下一组(一种索引+1-idea)就足够了。我已经搜索了所有内容,但我对如何做到这一点感到头疼。

如果我进入最后一组,结果会如何?有没有人建议如何做到这一点?

【问题讨论】:

    标签: arrays foreach swiftui


    【解决方案1】:

    找到了修复它的方法:)。受 E.Coms (SwiftUI - ForEach with Stride) 的启发,我用了下面的方法吧。

            List{
               ForEach(0 ..< self.dates.count) { index in
                    Section(header: Text("\(self.formatDate(date: self.dates[index].date!))")) {
                        ForEach(self.dates[index].timeArray, id: \.self) { time in
                            VStack {
                                     Text("\(String(time.timeMinutes))")
                             }
                        }
                        Text("Total time is \(self.values.filter{$0.date.date == self.dates[index].date}.map{$0.timeMinutes}.reduce(0, +))")
    
                        if index == (self.dates.count - 1) {
                            Text("No other values")
                        } else {
                            Text("Last month was: \(self.values.filter{$0.date.date == self.dates[index+1].date}.map{$0.timeMinutes}.reduce(0, +))")
                        }
                    }
                }
            }
    

    感谢 E.Com 和其他可能对此有所思考的人!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多