SwiftUI – 如何使用ForEach循环显示前一项和当前项的总和?

huangapple go评论53阅读模式
英文:

SwiftUI - How to display the total of the previous and current item with ForEach loop?

问题

我在下面的代码中添加了以下结果:

name: A amount: 100.50 	Total: 100.50

name: B amount: 50.52	Total: 151.02

name: C amount: 85.65	Total: 236.67
英文:

I am getting following result with the code added below:

name: A amount: 100.50 Total: 236.67

name: B amount: 50.52 Total: 236.67

name: C amount: 85.65 Total: 236.67

Instead, I would like to display the following results at each ForEach loop

name: A amount: 100.50 Total: 100.50

name: B amount: 50.52 Total: 151.02

name: C amount: 85.65 Total: 236.67

Below is the coding details:-

import SwiftUl

struct NameAmount: Identifiable {

let id = UUID()

let name: String
let amount: Double

}
struct ContentView: View {

let transactions: [NameAmount] = [
    NameAmount(name: "A", amount: 100.50),
    NameAmount (name: "B", amount: 50.52),
    NameAmount (name: "C", amount: 85.65)
]
var body: some View {
VStack {``
    List {
        VStack {
            ForEach(transactions) { item in
                HStack {
                    Text(item.name)
                    Text("(String (format: "%.2f", item.amount))")
                    Spacer()
                    Text("Total: (String(format: '%.2f", transactions.reduce(0)($0 +    $1.amount))")

}
}
}
}
}
}
}

Text("Total: (String(format: '%.2f", transactions.reduce(0)($0 + $1.amount))")
// the .reduce code is displaying the total of 236.67 at each foreach loop.

Instead, I would like to display the following results at each ForEach loop

name: A amount: 100.50 Total: 100.50

name: B amount: 50.52 Total: 151.02

name: C amount: 85.65 Total: 236.67

答案1

得分: -1

以下是您要翻译的内容:

如果你有当前的 `index` 并使用 `transactions` 的子集 `[0...index]`,你可以做到。

struct PartialSunView: View {
    struct NameAmount: Identifiable {
        
        let id = UUID()
        
        let name: String
        let amount: Double
        
    }
    
    let transactions: [NameAmount] = [
        NameAmount(name: "A", amount: 100.50),
        NameAmount(name: "C", amount: 50.52),
        NameAmount(name: "B", amount: 85.65)
    ]
    var filtered: [NameAmount]{
        transactions.sorted(using: KeyPathComparator(\.name))
    }
    var body: some View {
        VStack {
            List {
                VStack {
                    ForEach(Array(filtered.enumerated()), id:\.offset) { (index, item) in //获取当前索引
                        HStack {
                            Text(item.name)
                            Text(String (format: "%.2f", item.amount))
                            Spacer()
                            Text("Total: \(String(format: "%.2f", filtered[0...index].reduce(0){$0 + $1.amount}))")//使用子集
                            
                        }
                    }
                }
                
            }
        }
    }
}

请注意,我已将代码中的 HTML 实体编码 " 替换为正常的引号。

英文:

You can do it if you have the current index and use a subset of transactions [0...index]

struct PartialSunView: View {
    struct NameAmount: Identifiable {
        
        let id = UUID()
        
        let name: String
        let amount: Double
        
    }
    
    let transactions: [NameAmount] = [
        NameAmount(name: "A", amount: 100.50),
        NameAmount (name: "C", amount: 50.52),
        NameAmount (name: "B", amount: 85.65)
    ]
    var filtered: [NameAmount]{
        transactions.sorted(using: KeyPathComparator(\.name))
    }
    var body: some View {
        VStack {
            List {
                VStack {
                    ForEach(Array(filtered.enumerated()), id:\.offset) { (index, item) in //Get current index
                        HStack {
                            Text(item.name)
                            Text(String (format: "%.2f", item.amount))
                            Spacer()
                            Text("Total: \(String(format: "%.2f", filtered[0...index].reduce(0){$0 + $1.amount}))")//Use subset
                            
                        }
                    }
                }
                
            }
        }
    }
}

huangapple
  • 本文由 发表于 2023年3月9日 20:47:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定