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