4

我想在 iPad 上删除一行后显示空视图(此处:)。Text("Please select a person.")

当前: iPad 上的详细视图在删除项目后不会更新。 预期:在所选项目被删除后显示空视图。

struct DetailView: View {
    var name: String
    var body: some View {
        Text("Detail of \(name)")
    }
}

struct MainView: View {
    @State private var users = ["Paul", "Taylor", "Adele"]

    var body: some View {
        NavigationView {
            List {
                ForEach(users, id: \.self) { user in
                    NavigationLink(destination: DetailView(name: user)) {
                        Text(user)
                    }
                }
                .onDelete(perform: delete)
            }
            Text("Please select a person.")
        }
    }

    func delete(at offsets: IndexSet) {
        users.remove(atOffsets: offsets)
    }
}

来自Hacking With Swift的 NavigationView 示例。

在下面的示例中,详细视图在第一次启动后正确显示:here

但是删除一行后,之前选择的detail view(这里:)Paul仍然显示:这里

4

3 回答 3

0

从 iOS 14 开始,似乎不支持从导航视图的列表中删除元素。

NavigationLink类型采用isActive绑定,但在删除的情况下不起作用。当您收到.onDelete回调时,为时已晚。那NavigationLink不再在列表中,对您传递给它的绑定的任何更改都不会产生任何影响。

解决方法是将绑定传递给DetailView所有元素,以便它可以验证元素是否存在并相应地显示一些内容。

struct DetailView: View {
    var name: String
    @Binding var users: [String]

    var body: some View {
        if users.contains(name) {
            Text("Detail of \(name)")
        } else {
            EmptyView()
        }
    }
}

struct MainView: View {
    @State private var users = ["Paul", "Taylor", "Adele"]

    var body: some View {
        NavigationView {
            List {
                ForEach(users, id: \.self) { user in
                    NavigationLink(destination: DetailView(name: user, users: $users)) {
                        Text(user)
                    }
                }
                .onDelete(perform: delete)
            }
        }
    }

    func delete(at offsets: IndexSet) {
        users.remove(atOffsets: offsets)
    }
}
于 2021-05-24T17:26:20.207 回答
-1

您可以使用父视图的绑定来手动触发重新渲染(否则子视图不会得到通知):

import SwiftUI

struct DetailView: View {
  var name: String
  @Binding var notifier: Int
  @State var deleted: Bool = false
  
  var body: some View {
    Group {
      if !deleted {
        Text("Detail of \(name)")
          .onChange(of: notifier, perform: { _ in deleted = true })
      } else {
        Text("Deleted")
      }
    }
  }
}

struct MainView: View {
  @State private var users = ["Paul", "Taylor", "Adele"]
  @State private var deleteNotifier: Int = 0

  var body: some View {
    NavigationView {
      List {
        ForEach(users, id: \.self) { user in
          NavigationLink(destination: DetailView(name: user,
                                                 notifier: $deleteNotifier)) {
            Text(user)
          }
        }
        .onDelete(perform: delete)
      }
      Text("Please select a person.")
    }
  }

  func delete(at offsets: IndexSet) {
    users.remove(atOffsets: offsets)
    deleteNotifier += 1
  }
}
于 2021-04-21T17:49:01.527 回答
-2

您可以使用第二个选项来更新列表:index. 这样,它将加强刷新:

    var body: some View {
    NavigationView {
        List {
            ForEach(users.indices, id: \.self) { index in
                NavigationLink(destination: DetailView(name: self.users[index])) {
                    Text(self.users[index])
                }
            }
            .onDelete(perform: delete)
        }
        Text("Please select a person.")
    }
    }
于 2020-01-12T16:51:51.440 回答