这是我的简单应用程序.在那里我使用.onMove
方法来更改我的商品的顺序.但现在我只能改变顺序,但这并不能保存它(在新的位置上离开).因此,当我不再拿着我的物品时,它立即回到了原来的位置.你觉得我能怎么修好它?
import SwiftUI
struct HomeView: View {
struct Item: Identifiable {
var id = UUID()
var name: String
var color: Color
}
@State var items = [
Item(name: "Item 1", color: .red),
Item(name: "Item 2", color: .green),
Item(name: "Item 3", color: .blue),
Item(name: "Item 4", color: .brown)
]
var body: some View {
List {
ForEach(items, id: \.id) { item in
Text(item.name)
.frame(maxWidth: .infinity)
.padding()
.background(item.color)
.foregroundColor(.white)
}
.onMove(perform: move)
}
}
func move(from source: IndexSet, to destination: Int) {
items.move(fromOffsets: source, toOffset: destination)
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
}
}