如何知道在视图列表中按下了哪个按钮-SwiftUI

如何知道在视图列表中按下了哪个按钮-SwiftUI,swiftui,Swiftui,我有一个ResidentsView(),其中包含ResidentCell()的列表,每个单元格都有自己的按钮。此按钮在姓名上添加一条条纹。目前,它只增加了顶级驻留,因为我已经将其作为测试的默认驻留。但是,我希望当按下姓名下方的按钮时,此人的条纹会增加。我如何访问四个单元格中按下的按钮,以便知道我必须增加谁的条纹 struct ResidentCell: View { @State var showStripeView: Bool = false var resident:

我有一个
ResidentsView()
,其中包含
ResidentCell()
的列表,每个单元格都有自己的按钮。此按钮在姓名上添加一条条纹。目前,它只增加了顶级驻留,因为我已经将其作为测试的默认驻留。但是,我希望当按下姓名下方的按钮时,此人的条纹会增加。我如何访问四个单元格中按下的按钮,以便知道我必须增加谁的条纹

struct ResidentCell: View {

    @State var showStripeView: Bool = false


    var resident: Resident

    var body: some View {


        VStack {
            HStack{
                VStack(alignment: .leading) {
                    Text(resident.name)
                        .font(.title)
                    Text(String(resident.year))
                        .font(.caption)
                }.padding(.leading)

                Spacer()

                Text("Streepjes: \(numberOfStripes(stripes: resident.stripes))")
                    .font(.subheadline)
                    .fontWeight(.bold)
                    .padding(.trailing)
                    .foregroundColor(resident.stripes > 4 ? .red : .black)

            }

            Divider()
                .frame(width: 200)

            Button(action: {
                self.showStripeView.toggle()
                }) {
                Text("Add stripe")
                    .padding()
                    .border(Color.green, width: 5)
                    .foregroundColor(.black)
                    .cornerRadius(5)


                }
            .padding(.top, 5)
            .buttonStyle(BorderlessButtonStyle())
            .sheet(isPresented: $showStripeView) {
                AddStripeView(currentUser: Resident.testResidents[0])
            }
        }
    }

    func numberOfStripes(stripes: Int) -> String {
        let count = stripes

        var stripeString = ""

        for _ in (1...count) {
            stripeString.append("I")
        }

        return stripeString
    }

}

您想在按钮单击时增加resident.stripes的值,即“将其作为测试的默认驻留”是否表示常驻.testResidents[0]?您可以有一个@State整数,每个按钮都可以为其分配一个数组索引,然后将该索引传递给testResidents。