How to get UITableviewCell indexPath by UIButton Click in Swift

Let’s say you’ve subclassed UITableViewCell such that you have added a UIButton as a subview, and whenever you touch that button you want to perform some action on the data source of your UITableView. To do that you probably want to figure out what index path is associated with the pressed button.

Here’s one way, the idea is to add a target and action to the button of the cell for when it’s pressed, and then in that action get the index path by having the table view tell us what index path is associated with the point that was touched.

By Programmatically add target to UIButton

button.addTarget(self, action:#selector(self.buttonClicked:), for: .touchUpInside)

Getting the indexPath when press the button

func buttonClicked(_ sender: UIButton) {

        let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to:self.tableView)

        let indexPath = self.tableView.indexPathForRow(at: buttonPosition)

}

By Storyboard,
You have to create an IBAction with Button.

Now getting the IndexPath

@IBAction func didPressButton(_ sender: UIButton) {

        let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to:self.tableView)

        let indexPath = self.tableView.indexPathForRow(at: buttonPosition)

}

 

I hope it will help anyone. Happy coding.

That’s It. If you like this post, Please comment below or subscribe this blog for latest update.

Leave your comments

Advertisements

How to make UITextField move up when keyboard is present in Swift?

func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: Notification) {
    let info = notification.userInfo!
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    let keyboardHeight: CGFloat = keyboardSize.height
    let duration = info[UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval
    UIView.animate(withDuration: duration, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
        self.tableView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight, right: 0)
    }, completion: nil)
}
func keyboardWillHide(notification: Notification) {
    let info = notification.userInfo!
    let duration = info[UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval
    UIView.animate(withDuration: duration, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
        self.tableView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }, completion: nil)
}
func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

Leave your comments