SwiftUI ForEach with .indices() does not update after onDelete

雨燕双飞 提交于 2020-08-08 05:32:00

问题


my problem is: I have simple array with some Items. I want to display a List with these items using a ForEach with .indices(). (This is because my actual problem handles with Toggle in a List and for the isOn binding I need the index to address a model that is bound to an EnvironmentObject). So the solution to loop over the array items is no possible solution for my problem.

The simplified starting point looks like this:

struct ContentView: View {
    @State var items = ["Item1", "Item2", "Item3"]
    
    var body: some View {
        List {
            ForEach(items.indices) {index in
                Text(self.items[index])
            }.onDelete(perform: deleteItem)
        }
    }
    
    func deleteItem(indexSet: IndexSet) {
        self.items.remove(atOffsets: indexSet)
    }
}

If I now try to swipe-delete a row, I get this error message:

Thread 1: Fatal error: Index out of range

Debugging the index value inside the closure, I can see, that the indices of the items-array does not update. For example: If I delete the first row with "Item 1" and inspect the value of index after deleting the row it returns 2 instead of 0 (which is the expected first index of the array). Why is this and how can I fix this problem?

Thanks for your help!


回答1:


Just use dynamic content ForEach constructor (_ data: .., id: ...)

ForEach(items.indices, id: \.self) {index in   // << here !!
    Text(self.items[index])
}.onDelete(perform: deleteItem)


来源:https://stackoverflow.com/questions/62909572/swiftui-foreach-with-indices-does-not-update-after-ondelete

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!