I am trying to use the swipeActions SwiftUI modifier setup as displayed in the code below but the swipe action gets disabled as seen on this gif:
struct ContentView: View {
@ObservedObject var viewModel: ViewModel
var body: some View {
if viewModel.items.count > 0 {
ZStack {
List {
ForEach(viewModel.items, id: \.self) { item in
Text(item)
.swipeActions {
Button {
viewModel.removeAction(item: item)
} label: {
Text("Remove")
}
.tint(.orange)
}
}
}
}
} else {
ProgressView()
.foregroundColor(.accentColor)
.scaleEffect(2)
}
}
In the view model after the first swipe, I would reload the list from the API (the sample code just mocks a delay):
extension ContentView {
class ViewModel: ObservableObject {
@Published var items: [String]
init(items: [String]) {
self.items = items
}
func removeAction(item: String) {
if let index = items.firstIndex(where: { $0 == item }) {
items.remove(at: index)
}
let itemsSaved = items
items = []
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.items = itemsSaved
}
}
}
Expected behaviour: the reloaded rows do not have a space view at the beginning of each row, and the rows can be swiped as before.
Actual behaviour: each rows has a space view at the beginning of the row, you cannot swipe the rows as before.
I created a sample project also: code and additional video.
Any idea if there is workaround?
Thanks.