GOOGLE ADS

domingo, 1 de mayo de 2022

Las llamadas de SwiftUI que se pueden buscar en una lista combinan todos los elementos en una fila

In the example below the list items get squished all into one cell of the list, but only when calling.searchable() on it. When I call.searchable() on the text everything displays normally. I can't use it on the text though, because it has problems with the search filter.

import SwiftUI
struct FoodItem: Identifiable {
let id = UUID()
let name: String
}
struct ListView: View {

let listContent: [FoodItem] = [FoodItem(name: "Food 1"), FoodItem(name: "Food 2")]
@State public var searchQuery: String = ""

var body: some View {
NavigationView {
Form {
Section {
List(filteredContent) { item in
Text(item.name)
}
.searchable(text: $searchQuery)
}
}.navigationTitle("Food List")
}
}

var filteredContent: [FoodItem] {
if searchQuery.isEmpty {
return listContent
} else {
return listContent.filter { $0.name.localizedCaseInsensitiveContains(searchQuery)}
}
}
}
struct ListView_Previews: PreviewProvider {
static var previews: some View {
ListView()
}
}

Probablemente pasé dos horas buscando en línea e intentando arreglarlo y no encontré ningún resultado. Además, soy nuevo en SwiftUI.

¡¡Gracias!!


Solución del problema

El comentario de Paulw11 me ayudó mucho. Descubrí que ese no era el problema, que llamé a.searchable() en el texto, pero que tenía un error en mi filteredContent.

El problema se solucionó con esto:

var filteredContent: [FoodItem] {
if searchQuery.isEmpty {
return listContent
} else if (listContent.filter {$0.name.localizedCaseInsensitiveContains(searchQuery)}.isEmpty) {
return listContent
} else {
return listContent.filter { $0.name.localizedCaseInsensitiveContains(searchQuery)}
}
}

No hay comentarios:

Publicar un comentario

Regla de Firestore para acceder a la generación de subcolección Permisos faltantes o insuficientes

Tengo problemas con las reglas de Firestore para permitir el acceso a algunos recursos en una subcolección. Tengo algunos requests document...