这正在工作:
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)
}
})
}
所以我将其中的一部分移到了自己的View结构中,因为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)
})
}
问题在于,由于我将其移至其自己的View结构,因此模拟器不再像以前更改列表或其任何关系时那样显示列表中的更改。
最佳答案
您必须将您的TodoList作为@Binding
传递到TodoListLabel中。
struct TodoListLabel: View {
@Binding var todoList: TodoList
var body: some View {
// etc.
}
然后像这样初始化它:
TodoListLabel(todoList: $todoList)
这样,SwiftUI将知道每次TodoList项更改时都需要重绘TodoListLabel。
关于ios - 将FetchRequest项目移至其自己的View结构后,SwiftUI停止显示列表更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58786056/