是否可以让onDelete()
修饰符在ForEach
不位于 a 内的情况下工作List
?
例如在这个例子中,你怎么能得到默认的滑动来删除行为?
struct ContentView: View {
@State private var numbers = [Int]()
@State private var currentNumber = 1
var body: some View {
ScrollView {
VStack {
Button("Add Number") {
self.numbers.append(self.currentNumber)
self.currentNumber += 1
}
ForEach(numbers, id: \.self) {
Text("\($0)")
.padding()
.background(Color.green.opacity(0.3))
.clipShape(RoundedRectangle(cornerRadius: 10.0))
}
.onDelete { offsets in
numbers.remove(atOffsets: offsets)
}
}
}
}
}