将当前选中项的选择状态保存在列表中
Save the selection state of the current selected item on a list
处理本地文件的 macOS 应用程序需要在再次获得焦点时重新加载文件。这是为了加载应用程序在后台时可能放置在那里的任何新文件。
问题是,当我清除列表、重新加载文件(重建列表)时,第一项总是 selected,所以如果我正在处理某个项目,我将被迫select 再来一次,继续努力。
我尝试保留 selected 注释的当前 UUID 并将其传回,但我不知道这是否正确,也不知道如何以编程方式 select 匹配列表中的项目UUID 已保存。
如何保留当前 selected 项目,然后在重新加载数据时转到它?
我试过的代码:
struct DataItem: Codable, Hashable, Identifiable {
let id: UUID
var text: String
}
struct AllData: View {
@EnvironmentObject private var data: DataModel //array of DataItem's
@State var selectedItemId: UUID?
@State var currentItemId: UUID?
NavigationView {
List(data.prices.filter { searchText.isEmpty ? true : [=10=].text.localizedCaseInsensitiveContains(searchText) }) { price in
NavigationLink(
destination: PriceView(price: price, text: price.text),
tag: price.id,
selection: $selectedItemId
) {
VStack(alignment: .leading) {
Text(getTitle(titleText: price.text)).font(.body).fontWeight(.bold)
}
.padding(.vertical, 10)
}
}
}
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
self.currentItemId = self.selectedItemId
if getCurrentSaveDirectory(for: "savedDirectory") != "" {
if !isDirectoryEmpty() {
data.price.removeAll()
loadFiles(dataModel: data)
self.selectedItemId = self.currentItemId
}
}
}
}
我认为像您一样使用所选笔记的当前 UUID 是个好主意。
没有所有代码,很难测试我的答案,但是你可以
尝试这种方法,使用 List
selection
变量并在 onReceive
中添加简单代码,
比如这个示例代码:
struct AllData: View {
@EnvironmentObject private var data: DataModel //array of DataItem's
@State var selectedItemId: UUID?
@State var currentItemId: UUID?
@State var listSelection: DataItem? // <--- here selection
var body: some View {
NavigationView {
List(data.prices.filter { searchText.isEmpty ? true : [=10=].text.localizedCaseInsensitiveContains(searchText) },
selection: $listSelection) { price in // <--- here selection
NavigationLink(
destination: PriceView(price: price, text: price.text),
tag: price.id,
selection: $selectedItemId
) {
VStack(alignment: .leading) {
Text(getTitle(titleText: price.text)).font(.body).fontWeight(.bold)
}
.padding(.vertical, 10)
}
}
}
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
self.currentItemId = self.selectedItemId
if getCurrentSaveDirectory(for: "savedDirectory") != "" {
if !isDirectoryEmpty() {
data.price.removeAll()
loadFiles(dataModel: data)
self.selectedItemId = self.currentItemId
// --- here ---
if let theItem = data.first(where: {[=10=].id == selectedItemId}) {
listSelection = theItem
}
}
}
}
}
}
处理本地文件的 macOS 应用程序需要在再次获得焦点时重新加载文件。这是为了加载应用程序在后台时可能放置在那里的任何新文件。
问题是,当我清除列表、重新加载文件(重建列表)时,第一项总是 selected,所以如果我正在处理某个项目,我将被迫select 再来一次,继续努力。
我尝试保留 selected 注释的当前 UUID 并将其传回,但我不知道这是否正确,也不知道如何以编程方式 select 匹配列表中的项目UUID 已保存。
如何保留当前 selected 项目,然后在重新加载数据时转到它?
我试过的代码:
struct DataItem: Codable, Hashable, Identifiable {
let id: UUID
var text: String
}
struct AllData: View {
@EnvironmentObject private var data: DataModel //array of DataItem's
@State var selectedItemId: UUID?
@State var currentItemId: UUID?
NavigationView {
List(data.prices.filter { searchText.isEmpty ? true : [=10=].text.localizedCaseInsensitiveContains(searchText) }) { price in
NavigationLink(
destination: PriceView(price: price, text: price.text),
tag: price.id,
selection: $selectedItemId
) {
VStack(alignment: .leading) {
Text(getTitle(titleText: price.text)).font(.body).fontWeight(.bold)
}
.padding(.vertical, 10)
}
}
}
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
self.currentItemId = self.selectedItemId
if getCurrentSaveDirectory(for: "savedDirectory") != "" {
if !isDirectoryEmpty() {
data.price.removeAll()
loadFiles(dataModel: data)
self.selectedItemId = self.currentItemId
}
}
}
}
我认为像您一样使用所选笔记的当前 UUID 是个好主意。
没有所有代码,很难测试我的答案,但是你可以
尝试这种方法,使用 List
selection
变量并在 onReceive
中添加简单代码,
比如这个示例代码:
struct AllData: View {
@EnvironmentObject private var data: DataModel //array of DataItem's
@State var selectedItemId: UUID?
@State var currentItemId: UUID?
@State var listSelection: DataItem? // <--- here selection
var body: some View {
NavigationView {
List(data.prices.filter { searchText.isEmpty ? true : [=10=].text.localizedCaseInsensitiveContains(searchText) },
selection: $listSelection) { price in // <--- here selection
NavigationLink(
destination: PriceView(price: price, text: price.text),
tag: price.id,
selection: $selectedItemId
) {
VStack(alignment: .leading) {
Text(getTitle(titleText: price.text)).font(.body).fontWeight(.bold)
}
.padding(.vertical, 10)
}
}
}
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
self.currentItemId = self.selectedItemId
if getCurrentSaveDirectory(for: "savedDirectory") != "" {
if !isDirectoryEmpty() {
data.price.removeAll()
loadFiles(dataModel: data)
self.selectedItemId = self.currentItemId
// --- here ---
if let theItem = data.first(where: {[=10=].id == selectedItemId}) {
listSelection = theItem
}
}
}
}
}
}