0

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 } 

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Chris, thanks for the reply. I did set up the identifier. Everything works for the normal tableView, it just doesn't work when I try to search. The tableView for the search result is not showing the cells.
I have updated my answer. See if that works out for you.
I get "Binary operator == cannot be applied to operands of type "localCell" and nil ". That's because the cell is not optional, but if I change as! to as? it doesn't work either.
The issue is with the custom cell I created... if I change the cell to a UITableViewCell, then I get the result showing, but I need it to return a cell of type localCell.
1

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.

1 Comment

Can you add your code after fix it? I stuck here but another problem. When I input character in searchcontroller it show all table

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.