SwiftUI 列表视图不基于状态变量隐藏/取消隐藏元素

问题描述 投票:0回答:0

我有一个 NavigationLinks 列表。每个 NavigationLink 都由“isVisible”布尔检查包装。 另外,配置页面具有用于设置可见性打开或关闭的切换。

问题是当我从配置页面返回时,主视图不会根据所有项目的新可见性布尔值自行更新。即“如果(hv.isVisible)”没有重新评估。

我知道布尔值被持久化了。当我重新启动应用程序时,会显示列表中的正确项目

@ObservedObject fileprivate var viewModel = HealthValuesViewModel.instance
// in here values is defined as
// @Published var values: [HealthValue] = []
// which is then populated in init()
// The HealthValue is an ObservableObject class and has property
// @Published var isVisible: Bool

List(viewModel.values, id:\.type.rawValue) { hv in
    if (hv.isVisible) {
        NavigationLink(destination: hv.view) {
            HealthValueCellView(healthValue: hv )
        }
    }
}

我一直在玩@ObervedObject、@Published 等的各种组合。 还尝试确保我在列表中有一个 id。

非常感谢任何帮助或建议。

swiftui swiftui-list
© www.soinside.com 2019 - 2024. All rights reserved.