ios - 将 FetchRequest 项目移动到自己的 View 结构后,SwiftUI 停止显示列表更新
问题描述
这是有效的:
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
.
struct TodoListLabel: View {
@Binding var todoList: TodoList
var body: some View {
// etc.
}
然后像这样初始化它:
TodoListLabel(todoList: $todoList)
这样 SwiftUI 将知道 TodoListLabel 每次 TodoList 项目更改时都需要重新绘制。
推荐阅读
- url - 没有命名空间的Gitlab克隆URL?
- c# - 如何在机器人框架中分别使用对话框和表单流中的建议操作
- django - 使用 Django 的 ManifestStaticFilesStorage 时获取非散列 URL
- amazon-web-services - AWS RDS 和 pgAdmin4:无法连接到服务器:无法连接到服务器:连接超时
- split - 如何隔离四元数旋转的 x 和 z 分量?
- swift - 在 JSON 中解码可选值 - Swift 4
- c# - ScrollView 中的 WPF TreeView:如何在 TreeView 上启用鼠标滚动
- python-3.x - Python3网页抓取和解析Json
- linux - Logrotate 不旋转或创建新文件
- asp.net - 无法取消订阅 SignalR 事件