首页 > 解决方案 > SwiftUI 表格自定义滑动?

问题描述

有没有办法左右滑动表格行?我还没有找到新框架 SwiftUI 的东西,所以也许没有机会为此使用 SwiftUI?我需要删除行并使用自定义滑动

标签: swiftswiftui

解决方案


可以非常简单地实现删除操作和重新排序列表项的能力。

struct SwipeActionView: View {
    @State var items: [String] = ["One", "two", "three", "four"]

    var body: some View {
        NavigationView {
            List {
                ForEach(items.identified(by: \.self)) { item in
                    Text(item)
                }
                .onMove(perform: move)
                .onDelete(perform: delete)      
            }
            .navigationBarItems(trailing: EditButton())
        }
    }

    func delete(at offsets: IndexSet) {
        if let first = offsets.first {
            items.remove(at: first)
        }
    }

    func move(from source: IndexSet, to destination: Int) {
        // sort the indexes low to high
        let reversedSource = source.sorted()

        // then loop from the back to avoid reordering problems
        for index in reversedSource.reversed() {
            // for each item, remove it and insert it at the destination
            items.insert(items.remove(at: index), at: destination)
        }
    }
}

编辑:苹果有这篇文章,我不敢相信我以前没有找到。编写 SwiftUI 手势。我还没有尝试过它,但这篇文章似乎做得很好!


推荐阅读