【问题标题】:How to detect one button in tableview cell如何检测表格视图单元格中的一个按钮
【发布时间】:2018-08-04 19:53:38
【问题描述】:

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

表格视图

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

【问题讨论】:

  • 您能否附上视图的屏幕截图,并说明您所说的“它检测到多个按钮,(如奇数列表)”。
  • @Sam 我没有足够的积分所以无法截图
  • @Sam 给我邮件地址然后我把项目发给你
  • 你可以添加截图,但它不会嵌入图片内联,它只会让你添加链接。
  • @Sam 请提供链接。我在哪里添加屏幕截图

标签: ios swift uitableview uibutton


【解决方案1】:

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

1.使用UIButtonIBOutlets

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

示例:

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。使用UIButtonTag属性

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

示例:

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 的选中/未选中状态下设置不同的图像,您可以使用情节提要:

对于未选中状态:

对于选定状态:

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

【讨论】:

  • 您面临的问题是什么?让我知道您的完整问题陈述,并尽可能添加一些屏幕截图。
  • 请看我的问题。我不像你那样使用按钮
  • 我用的是二维数组
  • 你想知道按钮是在哪个 UITableViewCell 中按下的吗?
  • 我想在 UIButton 中设置图像。在选中和未选中之间会有两个图像。请再次查看我的代码,接下来尝试帮助我。请
【解决方案2】:

在您的cellForRowAt 方法中,为按钮添加标签号

cell.bookMarkBtn.tag = indexPath.row;

然后

@IBAction func bookMarkBtnAction(_ sender: UIButton) {

    sender.isSelected = !sender.isSelected

    if(sender.tag == 0)
    {
        ...
    } else if (sender.tag == 1)
    {
        ...
    }
}

【讨论】:

  • 请帮助我
【解决方案3】:

创建协议

 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 实现 HomeTableViewCellDelegate 方法

class HomeViewController: HomeTableViewCellDelegate {

     func bookMarkBtnTapped(btn: UIButton) {

 //        here btn is book mark button tapped by user from tableview cell
     }
}

【讨论】:

  • 现在你面临什么问题?
  • 我编辑了我的问题以便更好地理解,请查找它
【解决方案4】:

为此使用按钮标签。

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

从 tableviewcell 类中删除 @IBAction func bookMarkBtnAction(_ sender: UIButton)

【讨论】:

  • 但是按钮是由单元处理的,而不是视图控制器。请查看问题中的代码。
  • Ankur 请尝试根据 rmaddy 改正
  • 我今天做了两次这个方法,对我有用。您能否在添加这些行后添加更多详细信息?
  • @AnkurLahiry 添加此方法进行分页添加查看如何工作func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return self.view.frame.size.height } 我认为您可以做到。所以请再试一次
猜你喜欢
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多