所以我知道我的项目正在添加到“vitallist”(通过在终端中打印列表),但我没有看到它们出现在列表视图中。我认为这与“ObservedObject”没有正确链接有关。有什么建议么?
struct Vital: Identifiable {
let id = UUID()
var name: String
}
class VitalList:ObservableObject {
@Published var vitallist = [Vital]()
}
struct Row: View {
var vital: Vital
@State var completed:Bool = false
var body: some View {
HStack{
Image(systemName: completed ? "checkmark.circle.fill" : "circle").onTapGesture {
self.completed.toggle()
}
Text(vital.name)
}
}
}
struct Lists: View {
@ObservedObject var vitallist = VitalList()
var body: some View {
NavigationView{
List{
Section(header: Text("Vital")){
ForEach(vitallist.vitallist){ item in
Row(vital: item)
}
}
}
}
}
}