Reputation: 246
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
}
Upvotes: 0
Views: 213
Reputation: 246
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.
Upvotes: 1
Reputation: 2967
If your cell is returning nil
then create one yourself using the following after your dequeueReusableCellWithIdentifier
call:
if(cell == nil){
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "localCell")
}
Also, if you are using a UISearchController you should use 2 different arrays for populating your UITableView rather than manipulating the same array.
You can do this by checking whether the search controller is active inside your tableView delegate methods and acting accordingly.
Upvotes: 1