【问题标题】:SwiftUI list Move and select rowsSwiftUI list 移动和选择行
【发布时间】:2020-11-25 08:03:36
【问题描述】:

我正在尝试对列表行执行两个操作,如下图所示

第一个是通过使用“ForEach”和“.onMove”使其移动并重新排列它,这样就完成了,第二个是通过使用“List”和“selection”选项来制作多选行,所以也这样做了。 问题是我不能将这两个选项结合起来,因为其中一个使用“List”,另一个使用“ForEach”,当我这样做时,会发生意外行为。 有没有人可以帮我解决这个问题,拜托。

【问题讨论】:

    标签: swiftui-list


    【解决方案1】:

    这是一个可能的解决方案,如何结合选择和 onMove。

    struct ContentView : View {
            
        @State var items = ["Dave", "Tom", "Jeremy", "Luke", "Phil"]
        
        @State var selectedItems : Set<String> = Set<String>()
        
        var body : some View {
            NavigationView {
                List(selection: $selectedItems) {
                    ForEach(items, id:\.self) { item in
                        Text(item).tag(item)
                    }.onMove(perform: move)
                }
                .navigationBarItems(trailing: EditButton())
            }
        }
        
        func move(from source: IndexSet, to destination: Int) {
            items.move(fromOffsets: source, toOffset: destination)
        }
    }
    

    【讨论】:

      【解决方案2】:

      在我的应用程序中,davidev 的回答有点用。它允许选择多行,并且在开始移动时会出现句柄。但是,它不允许将选择放置到新位置(即,没有移动)。仍在研究为什么它实际上不会移动。

      【讨论】:

      • 这个答案更多是对已接受答案的评论。
      • 这并不能真正回答问题。如果您有其他问题,可以点击 进行提问。要在此问题有新答案时收到通知,您可以follow this question。一旦你有足够的reputation,你也可以add a bounty 来引起对这个问题的更多关注。 - From Review
      猜你喜欢
      • 2019-11-26
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2021-12-25
      • 2020-03-18
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多