Post

Replies

Boosts

Views

Activity

Reply to Filter array by String
Yes, groupArray is a dictionary. The complete code is: struct ResultView: View {   @State var websites : [Website]   @Binding var text : String   @State var groupedArray : [String: [Website]] = [:]   var body: some View {               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()                 }               })           }         }       }     }.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height, alignment: .center)        .navigationBarTitle("Results")           .colorScheme(.light)     .onAppear {                                       groupedArray = Dictionary(         grouping: websites,         by: {$0.name.first?.uppercased() ?? ""}       ).mapValues{$0.sorted()}                             }   }         } struct Loader : UIViewRepresentable{   func makeUIView(context: UIViewRepresentableContext<Loader>) -> UIActivityIndicatorView {     let indicator = UIActivityIndicatorView(style: .large)     indicator.startAnimating()     return indicator   }       func updateUIView(_ uiView: UIActivityIndicatorView, context: UIViewRepresentableContext<Loader>) {         } } I have this method from a tutorial, but there wasn't the filter method so I wrote it behind the keys in the ForEach. I don't know if thats right. The content is from a database but the array looks like this: @State var websites = [Website] = [Website(url: "https://www.apple.com/", name: "Apple", image: "Apple.png" , calls: 0), Website(url: "https://www.amazon.com/", name: "Amazon", image: "Amazon.png" , calls: 0)] Is that all you need to know?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20