gpt4 book ai didi

reactive-programming - 为什么我的 SwiftUI 列表行并不总是在内部更新

转载 作者:行者123 更新时间:2023-12-05 07:16:38 26 4
gpt4 key购买 nike

我有一个按完成和日期分组的提醒列表。数据来自名为 global 的 ObservedObject DataStore。我将 realmBinding 传递给单元格。单元格可以更新此绑定(bind),它会触发数据存储更新。

List {
// Past Due
if self.global.pastDueReminders.count > 0 {
Section(header: SectionHeader {}){
ForEach(self.global.pastDueReminders) { reminder in
NavigationLink(destination: ReminderDetail( reminder: reminder.realmBinding())) {
GeneralReminderCell(reminder: reminder.realmBinding())
}
}
}
}

// Completed
if self.global.completeReminders.count > 0 {
// Same as PastDue but for Completed
}
}

单元格看起来像:

struct GeneralReminderCell: View {
@Binding var reminder:Reminder

var body: some View {
HStack(alignment:.top, spacing: 10) {
Image(systemName: reminder.completed ? "checkmark.circle.fill" : "circle")
.onTapGesture(perform:{ self.reminder.completed = !self.reminder.completed })
VStack(alignment: .leading, spacing: 2) {
Text("Follow up with \(reminder.client.fullName)").fontWeight(.semibold)
if reminder.title.count > 0 {
Text(reminder.title)
}

Text(reminder.date.formatted()).foregroundColor(.gray)
}
}.padding(.vertical, 10)
}
}

当点击图像时,它会切换提醒完成状态,并且它在 ListView 中的位置会发生变化。完成后,点击的图像应更改为已填写的支票。这种行为几乎总是按预期发生,但有时选中的图像会与提醒的完成状态不同步。我已经研究了很长时间,但没有取得太大进展。为什么检查的图像并不总是与数据状态匹配?

最佳答案

尽管这是一个非常古老的问题,但我可能一直在研究看似相同的问题。就我而言,我的应用程序在 macOS 上运行。起初,这个问题似乎也非常间歇性,很难持续重现。

我还有一个 View 和一个 ForEachList 提供行。我的行的 View 包含一个用于可选 Image@State,它通过同一行 View< 执行的操作以几种不同的方式更新(例如 Continuity Camera、文件选择器或拖放)。问题是有时会显示新的 Image,有时不会显示。使用 Self._printChanges() 我能够看到 @State 正在改变并且正在重绘行的主体,但是,呈现的 View 不会改变。我能够观察到的唯一模式是,这个问题似乎只出现在 List 的最后一行。基于我下面的解决方法的成功,它似乎证实了 SwiftUI 的 List 重用表格单元格的方式存在问题。

我的解决方案/解决方法是替换:

List {
ForEach {
}
}

与:

ScrollView {
LazyVStack {
ForEach {
}
}
}

关于reactive-programming - 为什么我的 SwiftUI 列表行并不总是在内部更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59186046/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com