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

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

问题描述

如何检测 UITableviewCell 中的一个按钮,我在 UITableViewCell <中有10 UIButton / code>,接下来当我点击 UIButton 然后它会检测到多个按钮(就像奇数列表一样)。我的 UITableView 启用了分页功能。这是我的所有代码。



TableView

  class HomeViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {

@IBOutlet weak var homeTableView:UITableView!

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

覆盖func viewDidLoad(){
super.viewDidLoad()

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

FUNC numberOfSections(在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 {

让cell = tableView.dequeueReusableCell(withIdentifier:HomeTableViewCell,for:indexPath)为! HomeTableViewCell
返回单元格
}

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

TableViewCell

  class HomeTableViewCell:UITableViewCell {

@IBOutlet weak var bookMarkBtn:UIButton!

@IBAction FUNC bookMarkBtnAction(_发件人:UIButton的)!{

sender.isSelected = sender.isSelected

如果(sender.isSelected == true)
{
sender.setImage(UIImage(named:favorite_blue),for:UIControlState.normal)
}
else
{
sender .setImage(UIImage的(命名为: favorite_white),用于:UIControlState.normal)
}
}
}


解决方案

UITableViewCell中检测 UIButton ,您可以按照以下任何一种方法:



1。使用 UIButton IBOutlets



你可以在 UITableViewCell 中创建与 UIButton 对应的 IBOutlet 使用这些商店来识别执行的按钮操作。



示例:

  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具体代码在这里
}
else if if sender === button2
{
// button2特定代码在这里
}
//等等..
}
}

2。使用 UIButton 标记属性



你可以为 UITableViewCell UIButton 提供标记值$ c>然后使用该标记来标识特定按钮。



示例:

 类CustomCell:的UITableViewCell 
{
@IBAction FUNC onTapButton(_发件人:的UIButton)
{
如果sender.tag == 1
{
// button1标签= 1
//按钮1特定代码在这里
}
else if sender.tag == 2
{
// button2有一个标签= 2
// button2特定代码这里
}
//等等..
}
}

修改:



要在UIButton的选定/未选定状态下设置不同的图像,您可以使用故事板:



取消选择ed州:





对于选定状态:





<请告诉我你是否还有任何问题。


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

解决方案

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.

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

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