Hi,
In the following code, I filter an array, but it only works with the first letter of the name. For Example, when I write "A" in the TextField, the filter shows me "Apple". But if I write "Ap", the filter shows me nothing. Can you help me?
In the following code, I filter an array, but it only works with the first letter of the name. For Example, when I write "A" in the TextField, the filter shows me "Apple". But if I write "Ap", the filter shows me nothing. Can you help me?
Code Block Swift List{ ForEach(groupedArray.keys.sorted().filter{ text.isEmpty || $0.contains(text) }, id: \.self){key in Section(header: Text(key)) { ForEach(groupedArray[key]!, id: \.self){website in NavigationLink( destination: WebView(website: website, url: URL(string: website.url)), label: { Text(website.name) Spacer() if website.image != ""{ RemoteImage(url: website.image).frame(width: 25, height: 25, alignment: .center) }else{ Loader() } }) } } } }
In your way of creating groupedArray, its keys contains section titles.
["A", "B", ...]
If you want to filter rows by name, you need to move filter to the second ForEach:
You may want to remove empty sections, but that needs a little more complex code.
["A", "B", ...]
If you want to filter rows by name, you need to move filter to the second ForEach:
Code Block ForEach(groupedArray.keys.sorted(), id: \.self){key in //<- Section(header: Text(key)) { ForEach(groupedArray[key]!.filter{ //<- text.isEmpty || $0.name.contains(text) //<- }, id: \.self){website in //<- NavigationLink( destination: WebView(website: website, url: URL(string: website.url)), label: { Text(website.name) Spacer() if website.image != ""{ RemoteImage(url: website.image).frame(width: 25, height: 25, alignment: .center) }else{ Loader() } }) } } }
You may want to remove empty sections, but that needs a little more complex code.