这在起作用:
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结构中,因此模拟器不再像以前更改列表或其任何关系时那样显示列表中的更改。
答案 0 :(得分:0)
您必须将@Binding
的TodoList传递到TodoListLabel。
struct TodoListLabel: View {
@Binding var todoList: TodoList
var body: some View {
// etc.
}
然后像这样初始化它:
TodoListLabel(todoList: $todoList)
这样,SwiftUI将知道每次TodoList项更改时都需要重绘TodoListLabel。