The tableView doesn't show the cells with results from my search. The search is working fine, I verified all the data, but when I assign the cell.localName?.text to the value, it's always nil. Not sure what is wrong. I've found another post with same issue in Objective-C and the answer there was that I need to use the cell from the real tableView, by adding self.tableView when dequeueing the cell, but it didn't work for me. Any suggestions?
Here is my code:
class SearchResultsController: UITableViewController, UISearchResultsUpdating {
var localNames: [String] = []
var filteredNames: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerClass(localCell.self, forCellReuseIdentifier: "localCell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchedString = searchController.searchBar.text
filteredNames.removeAll(keepCapacity: true)
if !searchedString.isEmpty {
let filter: String -> Bool = {name in
let range = name.rangeOfString(searchedString, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range != nil
}
let matches = localNames.filter(filter)
filteredNames += matches
}
tableView.reloadData()
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return filteredNames.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: localCell = self.tableView.dequeueReusableCellWithIdentifier("localCell") as localCell
cell.localName?.text = filteredNames[indexPath.row]
println(cell.localName?.text)
cell.localAddress?.text = "text"
cell.scheduleData?.text = "text"
cell.salaData?.text = "text"
cell.wifiData?.text = "text"
return cell
}
Ok, so I found the solution and posting in case someone else will have the same issue as I had. I was using the custom cell from a prototype cell from storyboard. The resultController creates a new tableView that knows nothing about the custom cell, that's why it returns always nil. To fix it, I made a reference to the firstly used tableView and not a new one, or get the cell from a real table, this way my custom cell was recognised.