如何在每个循环的 list/for 中获取一行的 "edit" 按钮? [斯威夫特]

How do I get an "edit" button for a row in a list/for each loop? [SwiftUI]

我想进行编辑并添加一个按钮,如下图圈出的按钮一样。我需要这些按钮,以便我可以更改文件夹的属性。单击默认的 SwiftUI 编辑按钮时,如何使此按钮起作用?

我想要的:

(点击编辑按钮之前)

(点击编辑按钮后)

我目前拥有的:

(点击编辑按钮之前)

(点击编辑按钮后) 我的代码:

var body: some View {
        ZStack {
            List {
                ForEach(searchResults, id: \.self.id) { dir in
                    Section(dir.title) {
                        ForEach(dir.getChildFolders(), id: \.self.id) { folder in
                            NavigationLink(destination: DirectoryView(directory: folder)) {
                                Label(folder.title, systemImage: "folder")
                            }
                        }
                        .onDelete(perform: { offsets in
                            dir.items.remove(atOffsets: offsets)
                            updateView.update()
                        })
                        .onMove(perform: { source, destination in
                            dir.items.move(fromOffsets: source, toOffset: destination)
                            updateView.update()
                        })
                    }
                }
                
            }

需要自己显示按钮。

首先,确保您有一些 @State var 跟踪是否切换了编辑模式:

@State var editing: Bool = false

然后,在您的 body 中,显示 NavigationLink 旁边的圆圈按钮,如果您是 editing:

var body: some View {
    ZStack {
        List {
            ForEach(searchResults, id: \.self.id) { dir in
                Section(dir.title) {
                    ForEach(dir.getChildFolders(), id: \.self.id) { folder in
                        HStack {
                            NavigationLink(destination: DirectoryView(directory: folder)) {
                                Label(folder.title, systemImage: "folder")
                            }
                            if editing {
                                Spacer()
                                Button(action: {
                                    // Perform circle button action here
                                }) {
                                    Image(systemName: "ellipsis.circle")
                                }
                            }
                        }
                    }
                    .onDelete(perform: { offsets in
                        dir.items.remove(atOffsets: offsets)
                        updateView.update()
                    })
                    .onMove(perform: { source, destination in
                        dir.items.move(fromOffsets: source, toOffset: destination)
                        updateView.update()
                    })
                }
            }
        }
    }
}