如何使用SQLLite调用search tableview委托

时间:2017-07-27 20:50:20

标签: ios swift xcode sqlite uitableview

我准备了tableview。如下面的屏幕所示,有一个sqllite数据库。 。但搜索委托功能不起作用。

var dictionaries = [[String:AnyObject]]() //
var filteredDogs = [[String:AnyObject]]()
var searchController: UISearchController!
var resultsController = UITableViewController()
func updateSearchResultsForSearchController(searchController: UISearchController) {

    self.filteredDogs =  self.dictionaries.filter{(dict:[String:AnyObject]) -> Bool in

        if dict.lowercaseString.containsString(self.searchController.searchBar.text!.lowercaseString)
        {
            return true

        }
        else
        {
            return false

        } 
    }
    //Update results table


    self.resultsController.tableView.reloadData()
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! RowTableViewCell


    if tableView == self.tableView
    {
        let row = self.dictionaries[indexPath.row]
        cell.language1.text = row["foreign_language"] as? String
        cell.language2.text = row["native_language"] as? String


    }
    else
    {
        let row = self.filteredDogs[indexPath.row]
        cell.language1.text = row["foreign_language"] as? String
        cell.language2.text = row["native_language"] as? String


    }
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let row = self.dictionaries[indexPath.row]
    self.speechText((row["foreign_language"] as? String)!, language: (row["language"] as? String)!)

    let indexPath = self.tableView.indexPathForSelectedRow
    if((indexPath) != nil){
        self.tableView.deselectRowAtIndexPath(indexPath!, animated: true)
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! RowTableViewCell


    if tableView == self.tableView
    {
        let row = self.dictionaries[indexPath.row]
        cell.language1.text = row["foreign_language"] as? String
        cell.language2.text = row["native_language"] as? String


    }
    else
    {
        let row = self.filteredDogs[indexPath.row]
        cell.language1.text = row["foreign_language"] as? String
        cell.language2.text = row["native_language"] as? String


    }
    return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let row = self.dictionaries[indexPath.row]
    self.speechText((row["foreign_language"] as? String)!, language: (row["language"] as? String)!)

    let indexPath = self.tableView.indexPathForSelectedRow
    if((indexPath) != nil){
        self.tableView.deselectRowAtIndexPath(indexPath!, animated: true)
    }
}