swiftui - 使用 LazyVStack 在 Mac 应用程序中重新排序 SwiftUI 列表
问题描述
这是一个简单的macOS演示应用程序,它使用 SwiftUIList
和LazyVStack
. 我更喜欢使用 aLazyVStack
因为它更容易自定义样式。但似乎缺少一些内置功能,例如重新排序列表项(如下所示)
import SwiftUI
class MyData: ObservableObject{
@Published var items = ["One", "Two", "Three", "Four", "Five"]
}
struct ContentView: View {
@StateObject var model = MyData()
var body: some View {
HStack{
//--- List ---
VStack{
Text("List")
.fontWeight(.bold)
List{
ForEach(model.items, id: \.self){ item in
Text(item)
}
.onMove(perform: move)
}
}
//--- LazyVStack ---
VStack{
Text("LazyVStack")
.fontWeight(.bold)
ScrollView {
LazyVStack {
ForEach(model.items, id: \.self){ item in
Text(item)
}
.onMove(perform: move)
}
}
}
}
}
//Reorder List
func move(from source: IndexSet, to destination: Int) {
model.items.move(fromOffsets: source, toOffset: destination )
}
}
在示例中,项目的重新排序工作正常List
。但在LazyVStack
.
有没有办法让.onMove
功能在LazyVStack
实例中工作?还是我必须用 来实现一整套定制的.onDrag
东西NSItemProvider
?
解决方案
推荐阅读
- c++ - 有条件地使用用 C++ 编写的词法分析器中的字符
- terraform - AWS DMS 终端节点中通过 Terraform 的连接属性
- python - 从列表中生成具有特定频率的熊猫数据框中的列
- mysql - HiveQL 相当于 MySQL 的 TRUNCATE()?
- google-bigquery - Google Bigquery 客户端库中的 put 方法是否使用流式插入?
- python - Django/React - 带有 AWS 托管静态文件的静态 Main.js 文件
- node.js - Axios 400 React 中的错误请求
- database - MongoDB - 具有父子关系的数据库设计
- ios - iOS 13.3.1 dyld:库未加载
- c++ - RegCreateKeyExA 函数返回 ERROR_INVALID_PARAMETER(错误代码 87)