美文网首页
SwiftUI—如何调整记录在List列表里的顺序

SwiftUI—如何调整记录在List列表里的顺序

作者: anny_4243 | 来源:发表于2020-07-15 19:48 被阅读0次

原文链接:https://github.com/fzhlee/SwiftUI-Guide#-%E7%AC%AC8%E8%8A%82list-move-

本节课演示如何调整记录在列表里的顺序。

示例代码:

struct ContentView : View {
    @State var languages = ["Objective-C", "Swift", "Flutter"]

    var body: some View {
        NavigationView {
            List {
                ForEach(languages,id: \.self) { language in
                    Text(language)
                }
                .onMove { (source, destination) in //添加一个onMove方法,使列表里的记录可以被执行移动操作
                    self.languages.move(fromOffsets: source, toOffset: destination) //当列表里的记录被移动时,也需要移动数组里的元素,以保持界面和数据的同步变化
                }
            }
            .navigationBarTitle(Text("Edit Row"), displayMode: .large)
            .navigationBarItems(trailing: EditButton())
        }
    }

    func moveItem(from source: IndexSet, to destination: Int) {
        languages.move(fromOffsets: source, toOffset: destination)
        print(languages)
    }
}

相关文章

网友评论

      本文标题:SwiftUI—如何调整记录在List列表里的顺序

      本文链接:https://www.haomeiwen.com/subject/vobjhktx.html