我有一个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
}
}