Get the Indexpath from button action of one cell iOS swift

Solution:

If you had already added an action to one button or switch then you can easily get the indexPath for that button using the below function.Code


extension UITableView {

func indexPathForView(view: AnyObject) -> NSIndexPath? {
        let originInTableView = self.convert(CGPoint.zero, from: (view as! UIView))
        return self.indexPathForRow(at: originInTableView) as! NSIndexPath
    }
}


You can add the above extension for tableView and get the indexPath.You can call the function inside the button action like below.


    @IBAction func didTapOnSwitch(_ sender: AnyObject) {
        let swit = sender as! UISwitch
        let index = self.myPlacesTbl.indexPathForView(view: swit)
}

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift