SwiftUI:通过“拖动”将列表中的项目从 A 部分移动到 B 部分

Red*_*Mak 6 ios swift swiftui

我试图实现一些简单的事情:将项目从 A 部分拖到 B 部分,问题是 onMove 操作返回的是 Int 而不是索引,因此我无法获取新部分和新行索引。

func onMove(perform action: ((IndexSet, Int) -> Void)?) -> some DynamicViewContent
Run Code Online (Sandbox Code Playgroud)

谁能告诉我这是否可能?

这是一个示例代码:

struct ContentView: View {
  
  @State var categories: [Tree<String>] = [
      .init(
          value: "Clothing",
          children: [
              .init(value: "Hoodies"),
              .init(value: "Jackets"),
              .init(value: "Joggers"),
              .init(value: "Jumpers"),
              .init(
                  value: "Jeans",
                  children: [
                      .init(value: "Regular"),
                      .init(value: "Slim")
                  ]
              ),
          ]
      ),
      .init(
          value: "Shoes",
          children: [
              .init(value: "Boots"),
              .init(value: "Sliders"),
              .init(value: "Sandals"),
              .init(value: "Trainers"),
          ]
      )
  ]
  
    var body: some View {
        List {
            ForEach(categories, id: \.self) { section in
                Section(header: Text(section.value)) {
                    OutlineGroup(
                        section.children ?? [],
                        id: \.value,
                        children: \.children
                    ) { tree in
                        Text(tree.value)
                            .font(.subheadline)
                    }
                }
            }
            .onMove(perform: onMove)

        }.listStyle(SidebarListStyle())
    }
  
  private func onMove(source: IndexSet, destination: Int) {
    categories.move(fromOffsets: source, toOffset: destination)
  }
}
Run Code Online (Sandbox Code Playgroud)