swift - SwiftUI 表格自定义滑动?
问题描述
有没有办法左右滑动表格行?我还没有找到新框架 SwiftUI 的东西,所以也许没有机会为此使用 SwiftUI?我需要删除行并使用自定义滑动
解决方案
可以非常简单地实现删除操作和重新排序列表项的能力。
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 手势。我还没有尝试过它,但这篇文章似乎做得很好!
推荐阅读
- json - 我如何在颤动中从 json 获得响应
- operating-system - 分配给进程操作系统的内存量
- python - 在 for 循环函数退出的打印中,值 +1 比预期的要高
- node.js - 如何在您的猫鼬模式中引用另一个属性
- mysql - MySQL - 使用 RegEx 和数据库表 JOIN 更新表
- c# - 自定义 MediaTrasportControls 不可见
- python - 从不同大小的 NumPy 数组列表中计算平均值
- python - 如何使用 ctypes 在 python 中创建 char *buffer?
- python-3.x - 检查py文件是通过ide还是命令行运行
- tfs - 从包含本地更改的主创建分支,或从搁置集创建分支