0

How to delete a row from tableview using custom buttom

//CustomCell.swift

protocol FavoriteCellDelegate {
    func deleteButton(sender:CustomCell)
}

class FavoriteItemTableViewCell: UITableViewCell{
    var delegate: FavoriteCellDelegate!
    @IBAction func deleteButton(_ sender: UIButton) {
    delegate.deleteButton(sender: self)
}
}


CustomClass:UITableViewDataSource,UITableViewDelegate,CustomCellDelegate{
@IBOutlet weak var tableView: UITableView!
// all necessary functions for table view....

// Function delegated to perform action.
func deleteButton(sender:FavoriteItemTableViewCell){
    //How should I delete. How can I get index path here
}

}

Q. What should I write in the deleteButton function ? I am unable to get the indexPath here so what should I do instead. I already have another button in cell and the delegation is working fine.

3
  • 1
    indexPathForCell passing in sender Commented Jun 23, 2017 at 19:31
  • Do you have to use delegate on this? There are simpler ways to accomplish this requirement. Commented Jun 23, 2017 at 19:37
  • I tried using addTarget and it was giving an exception then I searched and someone on stackoverflow wrote that there is no other way but to use delegate. Commented Jun 24, 2017 at 7:38

1 Answer 1

1

you can get indexPath using table view point like this

let buttonPosition : CGPoint = sender.convert(sender.bounds.origin, to: tableview)
let indexPath = tableview.indexPathForRow(at: buttonPosition)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.