我在 SwiftUI 中使用 Xcode 11.1。
我使用以下代码实现了列表删除和排序功能。
如何使用滑动禁用行删除?
import SwiftUI
struct ContentView: View {
@State var fruits = ["Apple", "Orange", "Banana"]
var body: some View {
NavigationView {
List {
ForEach(fruits, id: \.self) { fruit in
Text(fruit)
}
.onMove(perform: move)
.onDelete(perform: delete)
}
.navigationBarItems(trailing: EditButton())
}
}
func delete(offsets: IndexSet) {
fruits.remove(atOffsets: offsets)
}
func move(source: IndexSet, destination: Int) {
fruits.move(fromOffsets: source, toOffset: destination)
}
}
Run Code Online (Sandbox Code Playgroud) swiftui ×1