在Swift中过滤元组

时间:2014-06-09 09:40:31

标签: swift uisearchdisplaycontroller ios8

我的代码很简单:

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
}

工作得很好。表已满,表搜索工作正常。现在我想使任务复杂化:

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")]

更改表格功能:

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
}

表格仍然正确填写。每行都有自己的图像。我无法对元组数组进行过滤。你能建议我如何修改函数filterContentForSearchText?

谢谢!

2 个答案:

答案 0 :(得分:1)

我建议您不要以这种方式(使用元组)将数据保存在数组中,而是使用适当的方式来表示您的数据:structclass

这是你应该用类

做什么的一个例子
class Recipe {
    var id = 0
    var name :NSString = ""
    var desc = ""
    var time = 0
    var img = ""

    init(id: Int, name: String, desc: String, time: Int, img: String) {
        self.id = id
        self.name = name
        self.desc = desc
        self.time = time
        self.img = img
    }
}


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

func filter (searchString: String) {
    searchResults = recipes.filter{ recipe in
        return (recipe.name.localizedCaseInsensitiveContainsString("\(searchString)"))
    }
} 

答案 1 :(得分:0)

同意@JJSaccolo这些结构应该是更好的代码清晰度和类型安全性。

但是,要回答原始问题,您可以使用:

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