SwiftUI:通过“拖动”将项目从列表从A节移动到B节

时间:2020-10-21 19:53:21

标签: ios swift swiftui

我正在尝试实现一些简单的操作:将一个项目从A部分拖到B部分,问题是onMove操作返回一个Int而不是一个索引,所以我无法获得新的部分和新的行索引。 / p>

func onMove(perform action: ((IndexSet, Int) -> Void)?) -> some DynamicViewContent

有人可以告诉我这是否可行吗?

这是示例代码:

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)
  }
}

0 个答案:

没有答案