Performing a segue with the TableViewCell IndexPath value

uitableviewcell segue programmatically
segue from uitableviewcell to viewcontroller swift 4
tableview didselectrowatindexpath segue swift
perform segue from tableview cell
uitableview single selection swift
uitableview single selection programmatically
segue from uitableviewcell to viewcontroller swift 5
pass data from uitableviewcell to viewcontroller

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:

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,

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

Segue from UITableViewCell Taps in Swift, In many apps that use a UITableView, when a UITableViewCell is tapped, the If you tap anywhere else in the cell, it starts playing that podcast, and goes to the We'll need a variable to place the Swift String of the blog name. Then, we'll save the row from the NSIndexPath returned from tableView. But we left one thing that was not discussed: data passing between scenes (i.e. view controllers) with segue. This is the second article of our Storyboards series. In the first tutorial, we introduced the Storyboards, which is a friendly feature in Xcode for designing user interface.

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]
                }
            }
    }

Push segue from UITableViewCell to ViewController in Swift, To create a segue from a UITableViewCell to another View controller, we'll Related Questions & Answers; Where and how to use static variable in swift? cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView. Now if you wish to perform some operation when the segue occurs,  They are all performing the same segue, but I would like the value passed to be different for each button. Ex: Array of ["1", "2", ], where Cell 1 - Passes "1" to HomeView, Cell 2 - Passes "2" to HomeView, etc. But right now, I have added a target for the button to segue,

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)

Handling Row Selection in a Table View, In order for your app to perform the action, it must know when the user taps the row and func tableView(_ tableView: UITableView, didSelectRowAt indexPath: the item's data model, and displays a checkmark in the row for packed items. The didSelectRowAtIndexPath code is wrong. sender is usually self (and will never be an indexPath). It will normally be a control, or a view, or a view controller. The second println suggests that there is a segue on your tableviewcell

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

pass row number outside "did select" |Apple Developer Forums, in tableView? I need it in other view controller to choose data depending on. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { ..but it completely blocks segue to next viewcontroller. This tutorial will show you how to configure table contents using the Storyboard and programmatically and how to present data from Swift Arrays.

Use of unresolved identifier 'performSegue' in Tableviewcell, _videos[indexPath.row].id performSegue(withIdentifier: I knew reason because perform segue can't use in table view cell but i dont have any that is a view controller (or another object that knows the view controller). Home · iOS & Swift Tutorials iOS Storyboards: Segues and More. In this tutorial, you’ll learn how to connect view controllers to a storyboard, programmatically trigger segues, control visual components and respond to the user interactions.

Using UITableView, Create a view controller, and drag a UITableView into the view, as shown below. return 5 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) In a real application, the index could be used to pull out corresponding data from Step 6: Create a Segue from the Cell to the Detail ViewController. It's a learning project where I have three options, one leading to a code-only segue, one leading to a manual segue, and one leading to an automatic segue. The manual segue is like yours, with a storyboard segue and the code call to performSegue

Pushing a segue with didSelectRowAtIndexPath : swift, programming language built using a modern approach to safety, performance, and software cellForRow(at: indexPath)! as UITableViewCell valueToPass = currentCell. performSegue(withIdentifier: savedRecipeSegue, sender: self) } func Between Ray Wenderlich's and Wayne Bishop's books on Algorithms & Data 

Comments
  • stackoverflow.com/a/41887007/5461400
  • Please look at stackoverflow.com/questions/46378515/…. A callback closure is a very easy and efficient solution.
  • @vadian, this works perfectly, thank you!
  • You have more than two options 😉.
  • @vadian Sir Ifor right now i have only two options in mind :D Please feel free to update answer I trust you