Elliot Elliot - 6 months ago 62
Swift Question

How can I filter an array of dictionaries in 'updateSearchResultsForSearchController' to search a UITableView with Swift

I've been going round in circles trying to implement search functionality in my UITableView.

I've followed loads of instructions here on stackoverflow and watched a ton of videos on YouTube and I feel like I know how to implement UISearchController now but i'm struggling how to actually filter my array in the

updateSearchResultsForSearchController
function.

I've managed to get search working if I have a simple array of string values as you see in most of the online examples for implementing search but I have an array of dictionaries and have no idea how to use
.filter
to get at the key/value pairs in the dictionary.

My peopleData array is an array of dictionaries that comes from a JSON file which looks like this:

{
"people": [
{
"ID" : "1",
"firstname" : "Bob",
"lastname" : "Smith",
"age" : 25,
"gender" : "Male"
},
{
"ID" : "2",
"firstname" : "Fred",
"lastname" : "Smith",
"age" : "52",
"gender" : "Male"
}
]
}





My view controller looks like this:

// Created by Elliot Wainwright on 26/02/2016.
// Copyright © 2016 Elliot Wainwright. All rights reserved.

import UIKit

class SelectPersonTableViewController: UITableViewController {

var myList:NSMutableArray = []
var personData:NSMutableArray = []
var filteredPeopleData:NSMutableArray = []

var searchController : UISearchController!
var resultsController = UITableViewController()

@IBOutlet var selectPersonTable: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

guard let path = NSBundle.mainBundle().pathForResource("PeopleData", ofType: "json") else {
print("error finding file")
return
}

do {
let data: NSData? = NSData(contentsOfFile: path)
if let jsonResult: NSDictionary =
try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {
personData = jsonResult["people"] as! NSMutableArray
}
} catch let error as NSError {
print("Error:\n \(error)")
return
}

self.resultsController.tableView.dataSource = self
self.resultsController.tableView.delegate = self

self.searchController = UISearchController(searchResultsController: self.resultsController)
self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.searchResultsUpdater = self
definesPresentationContext = true

selectPersonTable.reloadData()
}

func updateSearchResultsForSearchController(searchController: UISearchController) {
//Filter through the array

//---------------------------------------------
//I have no idea how to do this for my array!!!
//---------------------------------------------

//Update the results TableView
self.resultsController.tableView.reloadData()
}
// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.tableView {
return self.peopleData.count
} else {
return self.filteredPeopleData.count
}
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

if tableView == self.tableView {
for person in personData {
let thisPerson = personData.objectAtIndex(indexPath.row)
cell.textLabel?.text = thisPerson["person"] as? String
}
} else {
for person in filteredPeopleData {
let thisPerson = filteredPeopleData.objectAtIndex(indexPath.row)
cell.textLabel?.text = thisPerson["person"] as? String
}
}

return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
myList.addObject(personData.objectAtIndex(indexPath.row))
navigationController?.popViewControllerAnimated(true)
}

// MARK: - Navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var vc = segue.destinationViewController as! ViewController
vc.peopleList = myList
}
}





As you can see, In
ViewDidLoad
I get the content of the JSON file within 'people'
personData = jsonResult["people"] as! NSMutableArray
so I end up with an array of dictionaries.

Ideally, users would be able to type something in the UISearchBar and the array (and so the table) would be filtered to show any elements that contain any values that include part of what the user has typed. So typing "ith" would return both rows as 'ith' appears in 'Smith' for both elements in the array.

At very least, i'd like to be able to search on at least one key/value.

Answer

Try this:

let results = personData.filter({ person in
    if let firstname = person["firstname"] as? String, lastname = person["lastname"] as? String, query = searchController.searchBar.text {
        return firstname.rangeOfString(query, options: [.CaseInsensitiveSearch, .DiacriticInsensitiveSearch]) != nil || lastname.rangeOfString(query, options: [.CaseInsensitiveSearch, .DiacriticInsensitiveSearch]) != nil
    }
    return false
})
filteredPeopleData = NSMutableArray(array: results)

This filters people that have a matching firstname property. You could implement something similar for lastname.

Comments