小编cap*_*ara的帖子

SwiftUI 如何使用滑动禁用行删除?

我在 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

3
推荐指数
1
解决办法
1337
查看次数

标签 统计

swiftui ×1