UITableViewCell Buttons with action(带有动作的 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 逻辑中是首选的,它可以用于表格单元格中的其他元素,如 UISwitch
、UIStepper
等.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 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有动作的 UITableViewCell 按钮
基础教程推荐
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- 更改 UITableView 部分标题的颜色 2022-01-01
- UINavigationBar 隐藏按钮文本 2022-01-01
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01
- Firebase 云消息传递令牌未生成 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01
- iOS4 创建后台定时器 2022-01-01