I am new to Xcode and Swift but the understanding is coming. The problem right now is that I have a table view with a searchview in it. When I run the code, data is received from the api to the tableview correctly but when I try to make a search then its returns an empty list.
How can I correct my code?
My Controller:
var clientDetails = [Client]() var currentClientDetails = [Client]() func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return currentClientDetails.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "clients_list") as? ClientCellTableViewCell else { return UITableViewCell() } cell.nameLb.text = "Name: " + currentClientDetails[indexPath.row].CLNT_FULLNAME cell.emailLb.text = "Email: " + currentClientDetails[indexPath.row].CLNT_EMAIL cell.phonenumberLb.text = "Phone Number: " + currentClientDetails[indexPath.row].CLNT_PHONE cell.clientidLb.text = "ID: " + currentClientDetails[indexPath.row].CLNT_CODE return cell } override func viewDidLoad() { super.viewDidLoad() self.clientsTableView.delegate = self self.clientsTableView.dataSource = self fetchData() setUpSearchBar() alterLayout() } func alterLayout() { clientsTableView.tableHeaderView = UIView() // search bar in section header clientsTableView.estimatedSectionHeaderHeight = 50 // search bar in navigation bar //navigationItem.leftBarButtonItem = UIBarButtonItem(customView: searchBar) navigationItem.titleView = searchBar searchBar.showsScopeBar = false // you can show/hide this dependant on your layout searchBar.placeholder = "Search Client by Name" } private func setUpSearchBar() { searchBar.delegate = self } func fetchData(){ let myapiurl = URL(string: "https://fpay.com/api") guard let downloadURL = myapiurl else { return } URLSession.shared.dataTask(with: downloadURL) { data, urlResponse, error in guard let data = data, error == nil, urlResponse != nil else { DispatchQueue.main.async { let alert = UIAlertController(title: "Fofoofo Pay", message: "Please try again or check your internet connection", preferredStyle: .alert) let action = UIAlertAction(title: "Ok", style: .default, handler: nil) alert.addAction(action) self.present(alert, animated: true, completion: nil) } return } print("JSON downloaded") do { let decoder = JSONDecoder() let downloadedJson = try decoder.decode(Client_details.self, from: data) self.clientDetails = downloadedJson.client_details self.currentClientDetails = self.clientDetails DispatchQueue.main.async { self.clientsTableView.reloadData() } } catch { print(error) } }.resume() } // Search Bar func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { currentClientDetails = clientDetails.filter({ client -> Bool in switch searchBar.selectedScopeButtonIndex { case 0: if searchText.isEmpty { return true } return client.CLNT_FULLNAME.lowercased().contains(searchText.lowercased()) default: return false } }) clientsTableView.reloadData() } Client class:
Client_details: Codable { let client_details: [Client] init(client_details: [Client]) { self.client_details = client_details } } class Client:Codable { let CLNT_CODE:String let CLNT_FULLNAME:String let CLNT_PHONE:String let CLNT_EMAIL:String init(CLNT_CODE:String, CLNT_FULLNAME:String, CLNT_PHONE:String, CLNT_EMAIL:String) { self.CLNT_CODE = CLNT_CODE self.CLNT_FULLNAME = CLNT_FULLNAME self.CLNT_PHONE = CLNT_PHONE self.CLNT_EMAIL = CLNT_EMAIL }
clientDetailsand store the result incurrentClientDetailshowever there is no comparison taking place to create a proper return value.currentClientDetails = clientDetails.filter({ $0.contains(searchText.lowercased()})