滚动视图中的ForEach直到交互才更新

问题描述:

添加到数组中或从数组中删除时,除非用户与 ScrollView 进行交互,否则视图不会更新.

When adding to or removing from an array, the view does not update unless the user interacts with the ScrollView.

Model 是一个 ObservableObject ,它在应用程序生命周期的早期被声明为 StateObject ,然后作为 EnvironmentObject .

The Model is a ObservableObject that is declared as a StateObject early in the app lifecycle and then passed as a EnvironmentObject.

当添加到 store.profile!.tasks.append()或从视图中删除数据时,数据只是带有对象数组的自定义 Profile 对象除非用户滚动ScrollView,否则不会更新;我的意思是说1个像素.

The data is simply a custom Profile object with an array of objects, when adding to store.profile!.tasks.append() or removing from the view does not update unless the user scrolls the ScrollView; I mean literally by 1 pixel.

我尝试过的

  • 在LazyVStack或VStack中包装ForEach
  • 在VStack中包装NavigationLink
  • 确保尺寸为全高,以防需要重新计算

代码

class Profile: Identifiable, Codable, Equatable
    var tasks: [Task]
}


struct Task: Identifiable, Codable, Equatable {
    var createdBy: String
    var title: String
    var date: Date
}

class Store : ObservableObject {
    @Published var profile: Profile?
}

struct ListView: View {

@EnvironmentObject var store: Store

  var body: some View {
     GeometryReader { geometry in
        ZStack(alignment: .bottomTrailing) {
          ScrollView(.vertical, showsIndicators: false){
              ForEach(store.profile!.tasks.filter({ return $0.createdBy == store.profile!.uid}).indices, id: \.self) { index in
                 NavigationLink(destination: DetailView().environmentObject(store)) {
                      TasksRow().id(UUID()) 
                 }
             }
          }
           .frame(maxWidth: geometry.size.width, alignment: .center)
           .frame(maxHeight: geometry.size.height)
           .background(Color.gray)
       }
    }
 }

您的 Profile 是引用类型,因此在其中添加任务时,对profile的引用不会更改,因此不会发布工作.

Your Profile is a reference type, so when you append task in it the reference to profile is not changed, so published does not work.

改为使用配置文件的值类型(即 struct )

Use instead value type for profile (ie. struct)

struct Profile: Identifiable, Codable, Equatable
    var tasks: [Task]
}

现在将任务附加到配置文件将更改配置文件本身,发布者发送事件和视图将刷新.

now appending task to profile will change profile itself, the publisher send event and view will refresh.