0

I have a TableView with Persons. When I tap on a cell it performs a seque to another view.

Searching works. But when I search, cancel, tap on one cell, go back and want to search again, it crashes. EXC_BAD_INSTRUCTION.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    var cell = UITableViewCell()


    if let sdc = self.searchDisplayController {

        if sdc.active {

        if let c: AnyObject? = self.tableView.dequeueReusableCellWithIdentifier("CellPerson"){


            cell = c as UITableViewCell
            cell.textLabel.text = personsFiltered[indexPath.row]! 

            }
            else
            {
                cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
            }


        } else {

        if let c: AnyObject? = self.tableView.dequeueReusableCellWithIdentifier("CellPerson"){


            cell = c as UITableViewCell
            cell.textLabel.text = persons[indexPath.row]! 

            }
            else
            {
                cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
            }

    }


    return cell
}

1 Answers1

0

It's difficult to know what is causing the crash without further details of exactly where it is happening but the problem may be around the dequeuing lines from self.tableView. In this delegate call back func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath the tableView parameter may be from the searchDisplayController or the backing TableViewController. To ensure you dequeue from the correct tableView change self.tableView to tableView.

Also keep in mind that the UISearchDisplayController has been deprecated in iOS 8 to be replaced by UISearchController so if this is not related to that you many have other trouble soon.

Dallas Johnson
  • 1,546
  • 10
  • 13