带有动作的 UITableViewCell 按钮 [英] UITableViewCell Buttons with action

查看:17
本文介绍了带有动作的 UITableViewCell 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个自定义 UITableViewCell,其中包含三个按钮来处理购物车功能,加号、减号和删除按钮,我需要知道哪个单元格已被触摸.

我已经尝试使用标签解决方案",但由于单元格的生命周期,它无法正常工作.

谁能帮我找到解决办法?

提前致谢

解决方案

我在 UITableViewCell 的子类中使用单元格委托方法解决了这个问题.

快速概览:

1) 创建协议

protocol YourCellDelegate : class {func didPressButton(_ tag: Int)}

2) Subclass 你的 UITableViewCell(如果你还没有这样做的话):

class YourCell : UITableViewCell{var cellDelegate: YourCellDelegate?@IBOutlet 弱变量 btn:UIButton!//使用此方法连接单元格中的按钮@IBAction func buttonPressed(_ sender: UIButton) {cellDelegate?.didPressButton(sender.tag)}...}

3) 你的视图controller符合上面实现的YourCellDelegate协议.

class YourViewController: ..., YourCellDelegate { ... }

4) 设置委托,在定义单元格之后(以便重用).

let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as!你的细胞cell.cellDelegate = 自我cell.btn.tag = indexPath.row

5) 在同一个控制器中(您实现的 UITableView 委托/数据源在哪里),YourCellDelegate 协议中放置一个方法..p>

func didPressButton(_ tag: Int) {print("我按下了一个带有标签的按钮:(tag)")}

现在,您的解决方案不依赖于标签/数字.您可以根据需要添加任意数量的按钮,因此无论您要安装多少按钮,都可以通过委托获得响应.

这种协议委托方案在 iOS 逻辑中是首选的,它可以用于表格单元格中的其他元素,如 UISwitchUIStepper 等.p>

Hi I have a custom UITableViewCell with three buttons to handle a shopping cart function, Plus,Minus and Delete button and I need to know which cell has been touched.

I've already tried to use the "tag solution" but it isn't working due to the lifecycle of the cells.

Can anyone please help me find a solution?

Thanks in advance

解决方案

I was resolving this using a cell delegate method within UITableViewCell's subclass.

Quick overview:

1) Create a protocol

protocol YourCellDelegate : class {
    func didPressButton(_ tag: Int)
}

2) Subclass your UITableViewCell (if you haven't done so):

class YourCell : UITableViewCell
{
     var cellDelegate: YourCellDelegate?   
      @IBOutlet weak var btn: UIButton!
    // connect the button from your cell with this method
    @IBAction func buttonPressed(_ sender: UIButton) {
        cellDelegate?.didPressButton(sender.tag)
    }         
    ...
}

3) Let your view controller conform to YourCellDelegate protocol that was implemented above.

class YourViewController: ..., YourCellDelegate {  ... }

4) Set a delegate, after the cell has been defined (for reusing).

let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! YourCell
cell.cellDelegate = self
cell.btn.tag = indexPath.row

5) In the same controller (where is your implemented UITableView delegate/datasource), put a method from YourCellDelegate protocol.

func didPressButton(_ tag: Int) {
     print("I have pressed a button with a tag: (tag)")
}

Now, your solution is not tag / number dependent. You can add as many buttons as you want, so you are ready to get response via delegate regardless how many buttons you want to install.

This protocol-delegate solution is preferred in iOS logic and it can be used for other elements in table cell, like UISwitch, UIStepper, and so on.

这篇关于带有动作的 UITableViewCell 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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