0

目前,我的应用程序仅使用列表视图来显示文件夹(如下所示)。所有在线教程都建议对于动态内容(在我的例子中,一组文件夹将在用户添加新内容后更新),我应该在列表中使用 ForEach 循环。然而,我的应用程序的问题是我需要由导航视图(如下所示)组成的 ForEach 循环来无限工作——我需要一个允许无限多个披露组/子文件夹的 ForEach 循环。我没有在网上找到详细说明/展示如何使用 ForEach 循环创建列表的教程,其中子项位于披露组中。因此,我只坚持使用 List 视图,因为它包含一个“children”参数(如我的代码所示)。我需要动态 ForEach 循环的原因是我需要添加一个 onDelete 修饰符。

在此处输入图像描述

代码:


List(searchResults, id: \.self, children: \.subFolders, selection: $selection) { folder in
                Label(folder.title, systemImage: "folder")
                .swipeActions {
                    Button(role: .destructive) { folder.delete() } label: { Label("Delete", systemImage: "trash") }
                    Button {} label: { Label("Move", systemImage: "rectangle.portrait.and.arrow.right") }
                }
                
                NavigationLink(destination: DirectoryView(directory: folder)) { EmptyView() }
                .frame(width: 0)
                .opacity(0)
            }
            .listStyle(.automatic)
            .searchable(text: $searchText)
            NavigationLink(isActive: $newDirAlert.showNewItem) { DirectoryView(directory: Folder.parentFolders.last ?? Folder()) } label: { EmptyView() }

4

0 回答 0