0

I have an NSMutableArray that has data which I want to search that data with the UISearchResultsUpdating. With the code I have in the function updateSearchResultsForSearchController, everything is working fine. However there is a problem that if I type a letter, any letter, no tableviewcell pops up. It is just blank, it does not search anything up. Any help on this?

Two variables that are important.

The NSMutubaleArray variable

var toDoItems = NSMutableArray()

Another useful variable that is in code

var filteredAppleProducts = [String]()

Here is my updateSearchResultsForSearchController function

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()
}

Here is also my CellForRowatIndexPath as well as my numberOfRowsInSecction if it is any help.

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            
    }        
}
JOM
  • 8,139
  • 6
  • 78
  • 111
  • From the little info you gave and the little debugging: The `NSPredicate` is wrong. If we look at `tableView:cellForIndexPath:`, we can guess that it should be `format: "itemTitel CONTAINS[c] %@"`. `array` is a `NSArray` of toDoItem (aka `NSDictionary`), not an array of strings. So `array as! [String]` shouldn't work. You may have to do: `self.filteredAppleProducts = array. valueForKey(key:"itemTitel") as! [String])` . I don't speak Swift and gave only "hints" on the code but the logic should do it. – Larme Jun 14 '16 at 08:38
  • I see what you are saying, however ( self.filteredAppleProducts = array. valueForKey(key:"itemTitel") as! [String] ) is giving an error, stating "[AnyObject] has no member valueForKey". – Aryan Sharma Jun 14 '16 at 13:35
  • As well, the (format: "itemTitel CONTAINS[c] %@") gives back a Thread error stating "EXC_BAD_INSTRUCTION". – Aryan Sharma Jun 14 '16 at 13:38
  • And what's the error? `itemTitel` => `itemTitle` (because of a typo?, but I took it from your own code). Showing what looks like a `toDoItem` may be helpful. – Larme Jun 14 '16 at 13:40
  • "itemTitel" is correct, it just looks like the actual code is giving an error :) – Aryan Sharma Jun 14 '16 at 13:41

0 Answers0