How to detect one button in tableview cell

后端 未结 4 659
说谎
说谎 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 16:52

    To detect a UIButton in a UITableViewCell, you can follow any of the below approaches:

    1. Use UIButton IBOutlets

    You can create an IBOutlet corresponding to each UIButton in the UITableViewCell and use those outlets to identify which button action is performed.

    Example:

    class CustomCell: UITableViewCell
    {
        @IBOutlet weak var button1: UIButton!
        @IBOutlet weak var button2: UIButton!
        @IBOutlet weak var button3: UIButton!
        @IBOutlet weak var button4: UIButton!
        @IBOutlet weak var button5: UIButton!
    
        @IBAction func onTapButton(_ sender: UIButton)
        {
            if sender === button1
            {
                //button1 specific code here
            }
            else if sender === button2
            {
                //button2 specific code here
            }
            //and so on..
        }
    }
    

    2. Use UIButton Tag property

    You can provide a tag value to each of the UIButton present in the UITableViewCell and then use that tag to identify the specific button.

    Example:

    class CustomCell: UITableViewCell
    {
        @IBAction func onTapButton(_ sender: UIButton)
        {
            if sender.tag == 1
            {
                //button1 has a tag = 1
                //button1 specific code here
            }
            else if sender.tag == 2
            {
                //button2 has a tag = 2
                //button2 specific code here
            }
            //and so on..
        }
    }
    

    Edit:

    For setting different images in selected/unselected state of UIButton, you can use storyboard for that:

    For Unselected state:

    For Selected state:

    Let me know if you still face any issues.

    0 讨论(0)
  • 2020-11-28 16:53

    In your cellForRowAt method, do add tag number to the buttons

    cell.bookMarkBtn.tag = indexPath.row;
    

    Then

    @IBAction func bookMarkBtnAction(_ sender: UIButton) {
    
        sender.isSelected = !sender.isSelected
    
        if(sender.tag == 0)
        {
            ...
        } else if (sender.tag == 1)
        {
            ...
        }
    }
    
    0 讨论(0)
  • 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
         }
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题