【发布时间】:2021-06-27 22:56:49
【问题描述】:
当我单击顶部的单元格时,我只希望文本“单击”出现在该对象上。为什么我会在其他单元格上看到此文本?我该如何解决这个问题?
单元格视图
struct CellView: View {
@Binding var text: String
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 30)
.foregroundColor(Color(UIColor.secondarySystemBackground))
.frame(width: 300, height: 100)
Text(text)
}
}
}
内容视图
struct ContentView: View {
@State var text: String = ""
var body: some View {
VStack {
ForEach(0 ..< 5) { item in
Button(action: {
self.text = "Clicked"
}) {
CellView(text: $text)
}
}
}
}
}
【问题讨论】: