article

Friday, December 10, 2021

SwiftUI Searchable

SwiftUI Searchable

ContentView.swift
 
//
//  ContentView.swift
//  swiftuidev15ios
//
//  Created by Cairocoders
//

import SwiftUI

struct ContentView: View {

    @State var search = ""
    
    var body: some View {
        NavigationView {
            List {
                ForEach(datarecipe.filter{(self.search.isEmpty ? true : $0.name.localizedCaseInsensitiveContains(self.search))}, id: \.id) { info in
                    Text(info.name)
                        .font(.title)
                        .fontWeight(.bold)
                    
                    Image(info.image)
                        .renderingMode(.original)
                        .resizable()
                        .frame(height: 200)
                        .cornerRadius(10)
                }
            }.navigationTitle("Searchable")
                .searchable(text: self.$search)
            {
                ForEach(datarecipe, id:\.id) { info in
                    HStack {
                        Text(info.name)
                            .searchCompletion(info.name)
                    }
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Model.swift
 
//
//  Model.swift
//  swiftuidev15ios
//
//  Created by Cairocoders
//

import Foundation
 
struct Recipe : Identifiable {
     
    var id : Int
    var name : String
    var image : String
}
 
var datarecipe = [
 
    Recipe(id: 0, name: "Bistek Tagalog", image: "1"),
    Recipe(id: 1, name: "Boogie flight", image: "2"),
    Recipe(id: 2, name: "Sinigang Na Baboy", image: "3"),
    Recipe(id: 3, name: "Ginisang Togue", image: "4"),
    Recipe(id: 4, name: "Ginisang Munggo (Monggo)", image: "5"),
    Recipe(id: 5, name: "Pork Estofado (Sweet Pork Stew)", image: "6"),
    Recipe(id: 6, name: "Pata Tim", image: "7"),
    Recipe(id: 7, name: "Pancit Palabok", image: "8"),
]

Related Post