Martheli
Martheli

Reputation: 981

didSelectRowAt with two tables Swift

I have the below code of my didSelect row function and the goal is to have two tables that do different things based on which table row is touched. The first part works fine. If tableView 2 gets touched, the print line works and the row gets deselected but if tableView 3 gets selected the print is not getting called. Both tableViews have outlets in the viewController.

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if tableView == self.tableView2 {

            sorted = true

            print ("TEST")

            filteredDevices.removeAll()
            filteredOS.removeAll()
            filteredReported.removeAll()
            filteredMac.removeAll()
            filteredVersion.removeAll()



            let facility = defaults.object(forKey: "facility") as! String



        self.tableView.reloadData()

        tableView2.deselectRow(at: tableView2.indexPathForSelectedRow!, animated: true)

        }

        if tableView == tableView3 {

            sorted = true

            print ("test")

            filteredDevices.removeAll()
            filteredOS.removeAll()
            filteredReported.removeAll()
            filteredMac.removeAll()
            filteredVersion.removeAll()



            self.tableView.reloadData()
            print ("test")

            tableView3.deselectRow(at: tableView3.indexPathForSelectedRow!, animated: true)
        }



    }

Upvotes: 1

Views: 180

Answers (1)

Robert
Robert

Reputation: 6810

It's likely that you forgot to set the delegate for tableView3 in your TableViewController.

tableView3.delegate = self

Since tableView2 is working, you must already have a line like that for tableView2. Just add one for tableView3 and you should be good.

Upvotes: 1

Related Questions