0

我有一个 NSMutableArray,其中包含我想使用 UISearchResultsUpdating 搜索该数据的数据。使用我在函数中的代码updateSearchResultsForSearchController,一切正常。但是有一个问题,如果我输入一个字母,任何字母,都不会弹出 tableviewcell。它只是空白,它不会搜索任何内容。对此有什么帮助吗?

两个重要的变量。

NSMutubaleArray 变量

var toDoItems = NSMutableArray()

代码中的另一个有用变量

var filteredAppleProducts = [String]()

这是我的 updateSearchResultsForSearchController 函数

func updateSearchResultsForSearchController(searchController: UISearchController)
{
    self.filteredAppleProducts.removeAll(keepCapacity: false)                

    let searchPredicate = NSPredicate(format: "title CONTAINS[c] %@", searchController.searchBar.text!)
    let array = (toDoItems as NSArray).filteredArrayUsingPredicate(searchPredicate)
    self.filteredAppleProducts = array as! [String]

    tbl!.reloadData()
}

如果有任何帮助,这也是我的 CellForRowatIndexPath 以及我的 numberOfRowsInSecction。

cellForRowAtIndexPath

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

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

    if (self.resultSearchController.active)
    {
        cell.lbl.text = self.filteredAppleProducts[indexPath.row]                
        return cell
    }
    else
    {
        let toDoItem: NSDictionary = toDoItems.objectAtIndex(indexPath.row) as! NSDictionary
        cell.lbl.text = toDoItem.objectForKey("itemTitel") as? String                
        return cell            
    }            
}

numberOfRowsInSection

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

    if (self.resultSearchController.active)
    {
        return self.filteredAppleProducts.count
    }
    else
    {            
        return toDoItems.count            
    }        
}
4

0 回答 0