如何检测表格视图单元格中的一个按钮 [英] How to detect one button in tableview cell

查看:16
本文介绍了如何检测表格视图单元格中的一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测 UITableviewCell 中的一个按钮,我在 UITableViewCell 中有 10 个 UIButton,接下来当我点击 UIButton 然后它检测到多个按钮,(如奇数列表).我的 UITableView 启用了分页.这是我的所有代码.

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.

表格视图

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)
        }
    }
}

推荐答案

要检测 UITableViewCell 中的 UIButton,您可以采用以下任一方法:

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

1.使用 UIButton IBOutlets

您可以创建一个 IBOutlet 对应于 UITableViewCell 中的每个 UIButton 并使用这些 outlet 来识别执行哪个按钮操作.

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

示例:

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.使用 UIButton Tag 属性

2. Use UIButton Tag property

您可以为 UITableViewCell 中的每个 UIButton 提供一个 tag 值,然后使用该标签来识别特定按钮.

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.

示例:

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..
    }
}

要在 UIButton 的选中/未选中状态下设置不同的图像,您可以使用情节提要:

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

对于未选中状态:

对于选定状态:

如果您仍然遇到任何问题,请告诉我.

Let me know if you still face any issues.

这篇关于如何检测表格视图单元格中的一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆