0

In tableViewCell I have userNameLbl with name, userClgLbl with number. I want to search and show data in tableView either name search or number search.

If user search name - based on name I can show data in tableView.
If user search number - based on number I can show data in tableView.

But how to work with both name and number for single search bar. Actually here my data is dynamic from server and number is not phone number.

UISearchBarDelegate added to my class

let searchBar = UISearchBar()
var filteredData: [Any]!
@IBOutlet weak var listTblView: UITableView!

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return filteredData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // create a new cell if needed or reuse an old one
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell


    cell.userNameLbl.text = filteredData[indexPath.row] as? String
    cell.userClgLbl.text = clg_uniq[indexPath.row] as? String

    return cell

} 

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    let strArr:[String] = clg_uniq as! [String]
    filteredData = searchText.isEmpty ? clg_uniq : strArr.filter({(dataString: String) -> Bool in
        // If dataItem matches the searchText, return true to include it
        return dataString.range(of: searchText, options: .caseInsensitive) != nil
    })

    DispatchQueue.main.async {
        self.listTblView.reloadData()
    }
    if searchText == "" {
        DispatchQueue.main.async {
            searchBar.resignFirstResponder()
        }
    }
}

//Added these lines after json parsing 
self.filteredData = self.clg_uniq
self.listTblView.reloadData()

My example JSON data is

{"log" =     (
            {
        Name = "Name1";
        "clg_uniq" = 5c640e7b86e35;
    },
            {
         Name = "Name2";
        "clg_uniq" = <null>;
    },
            {
         Name = <null>;
        "clg_uniq" = 5c647af5d5c4d;
    },
            {
         Name = "Name4";
        "clg_uniq" = 5c647a0427253;
    },
            {
         Name = <null>;
        "clg_uniq" = <null>;
    },
            {
         Name = "Name6";
        "clg_uniq" = $cuniq";
    },
 )
}
Naresh
  • 16,698
  • 6
  • 112
  • 113

1 Answers1

1

Add following variables -

var logArray = [Dictionary<String, Any>]() // For all result
var searchedLogArray = [Dictionary<String, Any>]() // For filtered result
var searchActive = false // whenever user search anything

Replace UISearchBarDelegate -

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    searchActive = searchText.count > 0 ? true : false

    let namePredicate = NSPredicate(format: "Name CONTAINS[c] %@", searchText)
    let clgUniqPredicate = NSPredicate(format: "clg_uniq CONTAINS[c] %@", searchText)

    let compoundPredicate = NSCompoundPredicate.init(orPredicateWithSubpredicates: [namePredicate, clgUniqPredicate])

    searchedLogArray = logArray.filter({
        return compoundPredicate.evaluate(with: $0)
    })

    listTblView.reloadData()

}

Replace UITableViewDataSource -

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return searchActive ? searchedLogArray.count : logArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // create a new cell if needed or reuse an old one
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell

    let logDict = searchActive ? searchedLogArray[indexPath.row] : logArray[indexPath.row]

    // Name
    if let name = log["Name"] as? String{

      cell.userNameLbl.text = name

    }else{

      cell.userNameLbl.text = ""

    }


    // clg_uniq
    if let clgUniq = log["clg_uniq"] as? String {

         cell.userClgLbl.text = clgUniq

    }else{

         cell.userClgLbl.text = ""

    }


    return cell

}

I hope you are persing response as Dictionary<String, Any>

Let me know if you are still having any issue.

Amir Khan
  • 1,318
  • 1
  • 14
  • 39
  • hai your code working fine tank you. But I got some problem, if response has null values it's crashed. For that I changed code in **textDidChange** like _searchedLogArray = logArray.filter({ (log) -> Bool in let param1 = log["Name"] as? String let param2 = log["clg_uniq"] as? String return (param1)?.lowercased().contains(searchText.trimmingCharacters(in: .whitespaces).lowercased()) ?? false || (param2)?.lowercased().contains(searchText.trimmingCharacters(in: .whitespaces).lowercased()) ?? false })_ Now labels printing text – Naresh Apr 01 '19 at 07:14
  • @iOS let me check – Amir Khan Apr 01 '19 at 07:19
  • @iOS Check my updated answer and let me know whether you are still having any issue – Amir Khan Apr 01 '19 at 07:43
  • The code will be working on those which has both values name and clg_uniq. So what you want - if there is only one value exist still working right? – Amir Khan Apr 01 '19 at 07:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191008/discussion-between-ios-and-amir-khan). – Naresh Apr 01 '19 at 07:56
  • Here you please search $cuniq value, it's not searching anything. – Naresh Apr 01 '19 at 08:00
  • Happy coding :) – Amir Khan Apr 01 '19 at 09:05
  • Can you please explain this line for me Mobile CONTAINS[c] %@ – Naresh Apr 01 '19 at 09:13
  • actually I know about CONTAINS[c], but how **Mobile** can get total data – Naresh Apr 01 '19 at 09:14
  • @iOS there is "Name" I guess, no "Mobile" – Amir Khan Apr 01 '19 at 09:14
  • Actually name is one of my key value in response. But how it can search – Naresh Apr 01 '19 at 09:25
  • @iOS there are two parameter `Name` and `clg_uniq` so search will be taken place with the parameters. – Amir Khan Apr 01 '19 at 09:26
  • Ok , automatically it will take care of that. am I right – Naresh Apr 01 '19 at 09:28
  • 1
    @iOS yes these predicates deal with the parameters inside the array of dictionary. Doesn't matter whether its value is same as parameter name – Amir Khan Apr 01 '19 at 09:30
  • @ Amir Khan, can you solve this https://stackoverflow.com/questions/55373430/twitter-error-your-credentials-do-not-allow-access-to-this-resource – Naresh Apr 01 '19 at 10:32