Search code examples
iosswiftxcode6uistoryboardsegue

UITableView and Search Bar with Segue


I have 2 arrays. One for all vehicles, other one is filteredVehicles. filteredVehicles generating dynamically by search bar and it work well. But I can't make dynamically segue for next scene.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.performSegueWithIdentifier("DisplayViewSegue", sender: tableView)
    }

and

// In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "DisplayViewSegue" {
            let nextScene  =  segue.destinationViewController as! DisplayViewController


            if sender as! UITableView == self.searchDisplayController!.searchResultsTableView {
                let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()!
                let selectedVehicle = self.filteredVehicles[indexPath.row]
                nextScene.currentVehicle = selectedVehicle
            } else {
                let indexPath = self.tableView.indexPathForSelectedRow()!
                let selectedVehicle = self.vehicles[indexPath.row]
                nextScene.currentVehicle = selectedVehicle
            }


        }
    }

These are my methods which relating make an segue. When I click any table list object App is broking and getting this kind of error:

Could not cast value of type 'UITableViewCell' (0x10757f9f0) to 'UITableView' (0x10757a810). (lldb)


Solution

  • I believe you've set up a segue in your storyboard from the table view cell to your next scene. By doing this, your segue is performed automatically, and you do not have to call self.performSegueWithIdentifier("DisplayViewSegue", sender: tableView)

    The problem of course is that the sender is the table view cell and not the table view, as you'd prefer. You have a couple of options:

    1. remove the contents of your didSelectRowAtIndexPath, and inside prepareForSegue, obtain a reference to the table view some other way.
    2. update your storyboard by changing the segue to originate from the view controller itself, rather than the table view cell. This will enable you to call performSegueWithIdentifier inside your didSelectRowAtIndexPath method.