【发布时间】:2021-08-19 17:28:19
【问题描述】:
我有一个基于数组成员的NavigationLink 列表。当您单击列表项时,下一个视图会加载到右侧,显示该特定项的数据。
删除项目时,会显示下一个项目。但是,当您删除列表中的所有项目时,最后一个项目的数据仍留在加载的视图中。我试图查看选定的项目是否为零,或者检查是否选择了某些其他形式,或者列表至少有一个项目,但我不知道如何检查所有这些并卸载视图。
代码如下:
@State var selectedNoteId: UUID?
NavigationView {
List(data.notes) { note in
NavigationLink(
destination: NoteView(note: note),
tag: note.id,
selection: $selectedNoteId
) {
VStack(alignment: .leading) {
Text(note.text.components(separatedBy: NSCharacterSet.newlines).first!).font(.body).fontWeight(.bold)
Text(note.dateText).font(.body).fontWeight(.light)
}
.padding(.vertical, 10)
}
}
.listStyle(InsetListStyle())
.frame(minWidth: 250, maxWidth: .infinity)
Text("Select a note or create a new one...")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.navigationTitle("A title")
如何卸载NoteView 并返回显示Text("Select a note or create a new one...")
感谢您的帮助!
已编辑:
这就是我删除数据的方式:
func removeNote() {
if let selection = selectedNoteId,
let selectionIndex = data.notes.firstIndex(where: { $0.id == selection }) {
//print("DEBUG: delete item: \(selectionIndex)")
data.notes.remove(at: selectionIndex)
}
}
【问题讨论】:
-
这是否回答了您的问题stackoverflow.com/a/62907153/12299030?
-
谢谢,但没有。我的问题不是删除一个项目,或者从另一个视图中删除,而是,一旦 NavigationLinks 上的列表为空,因为我删除了那里的每个项目,卸载留下的
NoteView视图显示来自的文本最后一个(现在消失了)笔记。