原文链接: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)
}
}
网友评论