简体   繁体   中英

how do i get the indexpath of the button in the tableview cell

I have a button in my tableViewCell , that has a segue to another view controller . I'm trying to pass a PostArray[indexpath.row].postKey from the tableViewCell to the viewcontroller via prepare for segue method. I tried different approaches like getting the indexpath on touch position , but don't think that would be a definitive solution. Any suggestions, i can achieve this?

also, i have tagged my buttons in the cellForRowAt func

cellData.openBtn.tag = indexPath.row
cellData.openBtn.addTarget(self, action: #selector(openPressed(_:)), for: .touchUpInside)

Button pressed get index from tableview

@objc func openPressed(_ sender:UIButton){
     let index = sender.tag
      print(index)
}

Instead of creating a segue from your cell to controller, create controller to controller(so that you can call performSegue manually.).

Then do this,

@objc func openPressed(_ sender:UIButton){
     self.performSegue(withIdentifier: "YourIdentifer", sender: sender)
}

And in your prepareForSegue, do this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "YourIdentifer" {
        guard let destinationVC = segue.destination as? YourNextVC else { return }
        destinationVC.userSelectedIndex = (sender as! UIButton).tag 
    }
}

In Swift target/action is pretty old-fashioned and objective-c-ish , there is a better way.

In the custom cell add a callback property and an IBAction . Connect the IBAction to the button.

var callback : (() -> Void)?

@IBAction func openPressed(_ sender : UIButton) {
    callback?()
}

In cellForRow assign a closure to the callback property. In the closure call performSegue and pass the postKey as sender

let post = PostArray[indexpath.row]
cellData.callback = { 
    self.performSegue(withIdentifier: "NextIdentifier", sender: post.postKey)
}

In prepare(for segue get the key from the sender parameter and pass it to the destination controller

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
{
    guard segue.identifier == "NextIdentifier" else { return }
    let postKey = sender as! String // or whatever the static type is
    let nextViewController = segue.destination as! NextViewController
    nextViewController.postKey = postKey
}

The simple form works only if the order of the table view row will never change.

If cells can be inserted, deleted or moved you have to gather the actual index path when the closure is executed

var callback : ((UITableViewCell) -> Void)?

@IBAction func openPressed_ sender : UIButton) {
    callback?(self)
}

...

let post = PostArray[indexpath.row]
cellData.callback = { actualCell in
    let actualIndexPath = tableView.indexPath(for: actualCell)!
    let actualPost = self.PostArray[actualIndexPath.row]
    self.performSegue(withIdentifier: "NextIdentifier", sender: actualPost.postKey)
}

Replace NextIdentifier and NextViewController with the real values and delete the code lines and the action function in the question.

you can give a tag to cell button

cell.yourButton.tag = indexPath.row

and create IBAction in cell and get index path like

@objc func openPressed(_ sender:UIButton){
     let tag = sender.superview.tag
    //preform your segue code and pass array object to another view controller "PostArray[tag]"

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM