SwiftUI .onDelete 过滤列表时索引错误

SwiftUI .onDelete wrong index when list is filtered

这是我的代码:

import SwiftUI

struct IngredientsList: View {
    @EnvironmentObject var ingredientsStore: IngredientEdit
    @State private var searchText = ""
    
    var searchResults: [Ingredient] {
        if searchText.isEmpty {
            return ingredientsStore.items
        } else {
            return ingredientsStore.items.filter {
                [=10=].name.lowercased().contains(searchText.lowercased()) }
        }
    }
    
    var body: some View {
        NavigationView {
            List {
                ForEach (searchResults) { ingredient in
                    NavigationLink (destination: IngredientDetail(ingredient: ingredient)) {
                        IngredientRow(ingredient: ingredient)
                    }
                }
                .onDelete(perform: deleteItems)
            }
            .searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .automatic))
            .navigationTitle("Ingredients")
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
    
    func deleteItems(at offsets: IndexSet) {
        withAnimation {
                ingredientsStore.items.remove(atOffsets: offsets)

        }
    }
    
}

如果 searchText 为空,效果很好,但如果列表被过滤,则效果不佳。调试器显示 'offsets' 始终包含过滤列表中的索引(我在屏幕上看到的),例如它将是 ingredientsStore.items[1] 但 .remove 方法会将 [1] 传递给 ObservedObject其中 [1] 将对应于一个完全不同的项目,因此将从数组中删除一个错误的项目:(换句话说,过滤列表包含具有绝对值的索引,据我所知。 您的康复将不胜感激。 谢谢。

ingredientsStore.items.remove(atOffsets: offsets)

这里的 offsets 是 searchResults 的索引。

所以你应该从 searchResults 中获取 Ingredient 项目,然后从 ingredientsStore.items 中删除它。

试试这个:

for offset in offsets{
    if let index = ingredientsStore.items.firstIndex(searchResults[offset]) {
        ingredientsStore.items.remove(at: index)
    }
}