-1

I am using tableviewcell and within that cell, i am using two UIButtons i.e. btnView and btnEdit. When i tap on btnEdit, i want to get indexPath for this button cell. How do i get ?

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "DepartmentCell", for: indexPath) as! ShowDepttTVCell

            //Set action to buttons
            cell.btnView.addTarget(self, action: #selector(btnView), for: .touchUpInside)
            cell.btnEdit.addTarget(self, action: #selector(btnEdit), for: .touchUpInside)

            return cell
        }

        @objc func btnView()
        {
            let viewDepttVC = self.storyboard?.instantiateViewController(withIdentifier: "Detailed Department") as! DetailedDepttVC
            self.navigationController?.pushViewController(viewDepttVC, animated: true)
        }

        @objc func btnEdit()
        {
//I want to get the indexpath of this cell button.
            print("Selected iNdex  :  \(r)")
            print("Edit")
            let editDeptt = self.storyboard?.instantiateViewController(withIdentifier: "Add Department") as! AddDepartmentVC
            editDeptt.btnTag = 0
            //editDeptt.strDName =
            self.navigationController?.pushViewController(editDeptt, animated: true)

        }
Ayush Agrawal
  • 147
  • 2
  • 2
  • 9
  • You should take a look at this post https://stackoverflow.com/questions/20655060/get-button-click-inside-uitableviewcell – trungduc Apr 19 '18 at 07:07

2 Answers2

1

check below code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                    let cell = tableView.dequeueReusableCell(withIdentifier: "DepartmentCell", for: indexPath) as! ShowDepttTVCell
        cell.btnView.tag =  indexPath.row
        cell.btnEdit.tag =  indexPath.row
                    //Set action to buttons
                    cell.btnView.addTarget(self, action: #selector(btnView(sender:)), for: .touchUpInside)
                    cell.btnEdit.addTarget(self, action: #selector(btnEdit(sender:)), for: .touchUpInside)

        return cell
   }

@objc func btnView(sender: UIButton)
{
        let index = sender.tag 
        let indexPath = IndexPath(row: index, section: 0)
        let viewDepttVC = self.storyboard?.instantiateViewController(withIdentifier: "Detailed Department") as! DetailedDepttVC
                    self.navigationController?.pushViewController(viewDepttVC, animated: true)
                }

@objc func btnEdit(sender: UIButton)
{
        let index = sender.tag 
        let indexPath = IndexPath(row: index, section: 0)
        //I want to get the indexpath of this cell button.
                    print("Selected iNdex  :  \(r)")
                    print("Edit")
                    let editDeptt = self.storyboard?.instantiateViewController(withIdentifier: "Add Department") as! AddDepartmentVC
                    editDeptt.btnTag = 0
                    //editDeptt.strDName =
                    self.navigationController?.pushViewController(editDeptt, animated: true)

}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Moayad Al kouz
  • 1,342
  • 1
  • 9
  • 19
0

Two ways
first. cell.btnView.tag = indexPath.row
change @objc func btnView() to @objc func btnView(btn: UIButton) so you can use btn.tag

second. this is a way to know cell use UIResponder My blog

Magic
  • 115
  • 6