如果变量来自数组,则标签不会自动刷新。有什么具体原因吗?
@State private var categories: [ItemCategory] = getCategories()
@State private var isOn = true
Button(action: {
categories[1].chose = !categories[1].chose
}, label: {
Text(categories[1].chose ? "Add" : "Remove") // not automatically refreshed, only with view change (go to an other and then back)
})
Button(action: {
isOn = !isOn
}, label: {
Text(isOn ? "Add" : "Remove") // automatically refreshed
})
更新:
抱歉,我错过了 ItemCategory
class ItemCategory: Codable, Equatable, Identifiable, Hashable {
var name: String
var items: [Item]
var chose: Bool
var collapsed: Bool
}