I want to display a table with a text and an image on each row. Clicking on the image should toggle the picture. The table is associated with an array of instances of a class, which has a boolean property for storing the state of the image.
浮油似乎没有任何效果。
import SwiftUI
struct TestView: View {
@State private var items = [
Item(id: 1, text: "Eins", isClicked: false),
Item(id: 2, text: "Zwei", isClicked: true),
Item(id: 3, text: "Drei", isClicked: false)
]
var body: some View {
Table(items) {
TableColumn("Items") { item in
HStack {
Text(item.text)
Button(action: {
item.isClicked.toggle()
}) {
Image(systemName: item.isClicked ? "checkmark.circle.fill" : "circle")
}
}
}
}
}
}
#Preview {
TestView()
}
class Item: Identifiable {
var id: Int
var text: String
var isClicked: Bool
init(id: Int, text: String, isClicked: Bool) {
self.id = id
self.text = text
self.isClicked = isClicked
}
}
我有什么错误?