Ios 将FetchRequest项移动到其自己的视图结构后,SwiftUI停止显示列表更新

Ios 将FetchRequest项移动到其自己的视图结构后,SwiftUI停止显示列表更新,ios,swift,swiftui,Ios,Swift,Swiftui,这是有效的: ForEach(todoLists, id: \.self) {todoList in NavigationLink(destination: TodoItemView(todoList: todoList), label: { HStack { if (todoList.countChecked == todoList.countTotal) { Text(todoList.title!)

这是有效的:

ForEach(todoLists, id: \.self) {todoList in
    NavigationLink(destination: TodoItemView(todoList: todoList), label: {
        HStack {
            if (todoList.countChecked == todoList.countTotal) {
                Text(todoList.title!)
                    .foregroundColor(stringToColor(string: todoList.color!))
                    .strikethrough()
            }
            else {
                Text(todoList.title!)
                    .foregroundColor(stringToColor(string: todoList.color!))
            }
            Spacer()
            Text(String(todoList.countChecked) + " / " + String(todoList.countTotal))
                .foregroundColor(.gray)
                .font(.footnote)
        }
    })
}
因此,我将其中的一部分移到了它自己的视图结构中,因为Xcode编译器开始出现:

struct TodoListLabel: View {
    var todoList: TodoList

    var body: some View {
        HStack {
            if (todoList.countChecked == todoList.countTotal) {
                Text(todoList.title!)
                    .foregroundColor(stringToColor(string: todoList.color!))
                    .strikethrough()
            }
            else {
                Text(todoList.title!)
                    .foregroundColor(stringToColor(string: todoList.color!))
            }
            Spacer()
            Text(String(todoList.countChecked) + " / " + String(todoList.countTotal))
                .foregroundColor(.gray)
                .font(.footnote)
        }
    }
}
现在我有这个:

ForEach(todoLists, id: \.self) {todoList in
    NavigationLink(destination: TodoItemView(todoList: todoList), label: {
        TodoListLabel(todoList: todoList)
    })
}

问题是,由于我将其移动到了自己的视图结构中,模拟器不再像以前我更改列表或其任何关系时那样显示列表中的更改。

您必须将TodoList作为
@Binding
传递到TodoListLabel中

struct TodoListLabel: View {
    @Binding var todoList: TodoList

    var body: some View {
    // etc.
}
然后像这样将其初始化:

TodoListLabel(todoList: $todoList)

通过这种方式,SwiftUI将知道每次TodoList项更改时都需要重新绘制TODOLISTABLE。

但是
TodoList
不是
@State
变量,它来自
ForEach
循环。TodoList是如何声明的?看看我的代码,
ForEach(TodoList,id:\.self){todoList in
todoList
FetchedResults