Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

ios - How to detect one button in tableview cell

How to detect one button in UITableviewCell, I have 10 UIButton in UITableViewCell, next when I click on UIButton then it detects multiple buttons, (as like odd number list). my UITableView is with paging enabled. Here is my all code.

TableView

class HomeViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var homeTableView: UITableView!

 let mainArray = [["1","2","3","4"],["5","6","7","8"],["9","10","11","12"],["13","14","15","16"]]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.homeTableView.delegate = self
        self.homeTableView.dataSource = self
    }

    func numberOfSections(in tableView: UITableView) -> Int {

        return mainArray.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return mainArray[section].count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.view.frame.size.height
    }
}

TableViewCell

class HomeTableViewCell: UITableViewCell {

    @IBOutlet weak var bookMarkBtn: UIButton!

    @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)
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here

For Selected state:

enter image description here

Let me know if you still face any issues.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...