0

I have a UIButton within a TableViewCell and I would like to perform a segue when the UIButton is tapped, but I need to know the IndexPath.row value in the moment of perform the segue. Here more details:

enter image description here

Code block where I perform the segue:

}else if(segue.identifier == "toChat"){
            let chatController = segue.destination as! ChatViewController
            chatController.employeeSender = self.employeeData
            //Currently I have tried this, but indexPath comes nil
            if let indexPath = tableView.indexPathForSelectedRow {
            chatController.employeeReceiver = (self.searchController!.isActive) ? self.searchResults[indexPath.row] : self.employeeArray[indexPath.row]
                print("Receiver Data\(self.employeeArray[indexPath.row].alias)")
            }

        }

Thank you,

Andoni Da Silva
  • 1,161
  • 15
  • 31

4 Answers4

0

You have two options

1) Easy

You can add tag to your button in the cellForRowAtindexPath and when you got tapped event of button you can create indexPath with that.

2) Create Subclass you UIButton and have one IndexPath property in it. and assign indexpath in cellForRow now you can access button.indexPath when button is tapped

Don't forgot to pass event to your viewcontroller with delegate or clouser

Hope it is helpful

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
0

Try this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! yourcustomcell
        cell.yourbutton.tag = indexPath.row
        cell.yourbutton.addTarget(self, action: #selector(self.chatbutton), for: .touchUpInside)
        return cell
 }

// Button Click
 @objc func chatbutton(_ sender: UIButton) {
        self.performSegue(withIdentifier: "toChat", sender: sender.tag)
 }


 // MARK: - prepare segue

   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "toChat" {
                let chatController = segue.destination as! ChatViewController
                chatController.employeeSender = self.employeeData
                if let row = sender as? Int  {
                    chatController.employeeReceiver = (self.searchController!.isActive) ? self.searchResults[row] : self.employeeArray[row]
                }
            }
    }
iParesh
  • 2,338
  • 1
  • 18
  • 30
0

you can also get the value of the cell's indexPath from which the button is tapped using the following extension.

public extension UITableView {

    func indexPathForView(_ view: UIView) -> IndexPath? {
        let center = view.center
        let viewCenter = self.convert(center, from: view.superview)
        let indexPath = self.indexPathForRow(at: viewCenter)
        return indexPath
    }
}

and get the indexPath in your @IBAction method of the button

let indexPath = tableView.indexPathForView(sender)
Ajay saini
  • 2,352
  • 1
  • 11
  • 25
0
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! yourcustomcell
        cell.button.tag = indexPath.row
        cell.button.addTarget(self, action: #selector(self.buttonTapped), for: .touchUpInside)
        return cell
 }

// Button Click
 @objc func buttonTapped(_ sender: UIButton) {
        pushToChatController(self.employeeReceiverArray[sender.tag])
 }


 func pushToChatController(aEmployeeReceiver: EmployeeReceiver) {
        let chatController: ChatViewController = UIStoryboard.chatStoryboard().instantiateViewController(withIdentifier: kChatViewControllerStoryboardId) as! ChatViewController
        chatController. employeeReceiver = aEmployeeReceiver
        navigationController?.pushViewController(chatController, animated: true)
  }

//NOTE: class func chatStoryboard() -> UIStoryboard { return UIStoryboard(name: “Chat”, bundle: nil) }

Let kChatViewControllerStoryboardId = “kChatViewControllerStoryboardId” //Storyboard Identifier

CrazyPro007
  • 1,006
  • 9
  • 15