Search code examples
swiftuisearchdisplaycontrollerios8

Filtering Tuple in Swift


I have simple code:

var recipes = ["Shrimp with garlic", "Napoleon cake", "Beef steak"]
var searchResults = String[]()

func filterContentForSearchText (searchText: String) {
    searchResults = recipes.filter{ ($0 as NSString).localizedCaseInsensitiveContainsString("\(searchText)") }
}

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
    self.filterContentForSearchText (searchString)
    return true
}

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    if tableView == self.searchDisplayController.searchResultsTableView {
        return searchResults.count
    } else {
        return recipesDict.count
    }
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell! {
    var cell = tableView.dequeueReusableCellWithIdentifier("SimpleTableCell") as? UITableViewCell

    if !cell {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "SimpleTableCell")
    }

    if tableView == self.searchDisplayController.searchResultsTableView {
        cell!.textLabel.text = searchResults[indexPath.row]
        cell!.image = UIImage(named: "food.jpg")
    } else {
        cell!.textLabel.text = recipes[indexPath.row]
        cell!.image = UIImage(named: "food.jpg")
    }
    return cell
}

It was work good. Table is full, table search works. Now I want to complicate the task:

var recipesDict = [(id: 1, name: "Shrimp with garlic", desc: "Lorem Ipsum....", time: 15, img: "shrimp.jpg"), 
(id: 2, name: "Napoleon cake", desc: "Lorem Ipsum....", time: 120, img: "napoleon.jpg"), 
(id: 3, name: "Beef steak", desc: "Lorem Ipsum....", time: 15, img: "steak.jpg")]

Change table functions:

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    if tableView == self.searchDisplayController.searchResultsTableView {
        return searchResults.count
    } else {
        // return recipes.count
        return recipesDict.count
    }
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell! {
    var cell = tableView.dequeueReusableCellWithIdentifier("SimpleTableCell") as? UITableViewCell

    if !cell {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "SimpleTableCell")
    }

    if tableView == self.searchDisplayController.searchResultsTableView {
        cell!.textLabel.text = searchResults[indexPath.row]
        cell!.image = UIImage(named: "food.jpg")
    } else {
        var currentRecipe = recipesDict[indexPath.row]
        NSLog ("Текущий рецепт : \(currentRecipe)")
        cell!.textLabel.text = currentRecipe.name
        cell!.image = UIImage(named: currentRecipe.img)
        // cell!.textLabel.text = recipes[indexPath.row]
        // cell!.image = UIImage(named: "food.jpg")
    }
    return cell
}

The table is still filled in correctly. Each row have your own image.I can't make filter for array of tuples. Can you suggest how I must modify the function filterContentForSearchText?

Thanks!


Solution

  • Agree with @JJSaccolo that these should be structs for better code clarity and type safety.

    However, to answer your original question, you can use:

    recipes.filter{ $0.name.localizedCaseInsensitiveContainsString("\(searchText)") }