Get the IndexPath from inside a UITableViewCell subclass in Swift Get the IndexPath from inside a UITableViewCell subclass in Swift ios ios

Get the IndexPath from inside a UITableViewCell subclass in Swift


Sounds like you need a delegate:

Delegates in swift?

Then just pass the cell itself as a parameter to the delegate, and then you can easily do tableView.indexPathForCell(cellFromDelegateMethod)


Hey you can use "Tag" of the button also.Inside the cellForRowAt method of table delegate u can tag the button with Indexpath.row . here is the example what i m tried to say.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {// get ur cell nib .As it has a buttoncell.startOrConntinuBtn.addTarget(self, action: #selector(sumbitOrContinue), for: .touchUpInside) cell.startOrConntinuBtn.tag = indexPath.row }

and in the touch method "sumbitOrContinue" -

 func sumbitOrContinue(sender: UIButton!) { let tag = sender.tag// do what you want to do like this examplelet detail = self.detailList[tag]let vc  = self.storyboard?.instantiateViewController(withIdentifier: "mockExamInt") as! MockWindowVcvc.detailId = detail.idself.navigationController?.pushViewController(vc, animated: true)}


UIButton.Type really does not have member superview, but sender have

var cell: UITableViewCell = sender.superview.superview as UITableViewCell