How to detect one button in tableview cell

后端 未结 4 679
说谎
说谎 2020-11-28 16:14

How to detect one button in UITableviewCell, I have 10 UIButton in UITableViewCell, next when I click on UIButton then i

4条回答
  •  旧时难觅i
    2020-11-28 17:11

    Use button tag for this.

    In tableViewController

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
        cell.bookMarkBtn.tag = indexPath.row
    
        cell.bookMarkBtn.addTarget(self, action: #selector(self. bookMarkBtnAction), for: .touchUpInside)
        return cell
    }
    
    
    
    @objc func bookMarkBtnAction(sender: UIButton) {
       if sender.tag == 0 { //or which indexpath do you want. 
         //code 
       } else if sender.tag == 1 {
        //code
       }  
      ..
     } 
    

    remove @IBAction func bookMarkBtnAction(_ sender: UIButton) from tableviewcell class

提交回复
热议问题