本节课演示如何给一个列表,添加同时支持删除和移动记录的功能。
示例代码:
struct ContentView : View { @State var languages = ["Objective-C", "Swift", "Flutter"] var body: some View { NavigationView { List { ForEach(languages, id: \.self) { language in Text(language) } .onDelete(perform: deleteItem) .onMove(perform: moveItem) } .navigationBarTitle(Text("Edit Row"), displayMode: .large) .navigationBarItems(trailing: EditButton()) } } func deleteItem(at offsets: IndexSet) { if let first = offsets.first { languages.remove(at: first) } } func moveItem(from source: IndexSet, to destination: Int) { languages.move(fromOffsets: source, toOffset: destination) } }