How to detect one button in tableview cell

后端 未结 4 677
说谎
说谎 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条回答
  •  无人及你
    2020-11-28 17:09

    Create protocol

     protocol HomeTableViewCellDelegate {
        func bookMarkBtnTapped(btn: UIButton)
     }
    
    
    class HomeTableViewCell: UITableViewCell {
    
       @IBOutlet weak var bookMarkBtn: UIButton!
    
       //add delegate var for protocol
       var delegate: HomeTableViewCellDelegate?
    
       @IBAction func bookMarkBtnAction(_ sender: UIButton) {
    
        sender.isSelected = !sender.isSelected
    
        if(sender.isSelected == true)
        {
            sender.setImage(UIImage(named:"favorite_blue"), for: UIControlState.normal)
        }
        else
        {
            sender.setImage(UIImage(named:"favorite_white"), for: UIControlState.normal)
        }
    
        //set this which button is pressed
        self.delegate?.bookMarkBtnTapped(btn: sender)
       }
    }
    

    HomeViewController implement HomeTableViewCellDelegate method

    class HomeViewController: HomeTableViewCellDelegate {
    
         func bookMarkBtnTapped(btn: UIButton) {
    
     //        here btn is book mark button tapped by user from tableview cell
         }
    }
    

提交回复
热议问题