【问题标题】:Detecting Which button was pressed within a custom UITableCell检测在自定义 UITableCell 中按下了哪个按钮
【发布时间】:2019-08-15 21:03:18
【问题描述】:

我目前有一个自定义单元格,其中有 3 个按钮通过IBOutlet 连接到我的自定义单元格。我遇到的问题是在测试期间,当我按下单元格内的任何按钮时,所有按钮都被按下。我认为这是由于我在按下每个按钮时设置处理的方式造成的:

cell.FTCOne.tag = indexPath.section
cell.FTCOne.addTarget(self, action: #selector(oneSelected), for: .touchUpInside)

cell.FTCTen.tag = indexPath.section
cell.FTCOne.addTarget(self, action: #selector(tenSelected), for: .touchUpInside)

cell.FTCTwenty.tag = indexPath.section
cell.FTCOne.addTarget(self, action: #selector(twentySelected), for: .touchUpInside)

我认为这是因为它们都具有相同的标签。如果是这种情况,识别用户选择的行的indexPath 的最佳方法是什么?

【问题讨论】:

  • 以小写字母开头的变量名
  • 您为FTCOne 拨打了三次addTarget
  • FWIW,我不建议使用tag 来捕获section。如果您稍后插入一个部分怎么办?最好将 UITableViewCell 引用传递回视图控制器,然后让它确定该单元格的部分(和行)。

标签: swift uitableview


【解决方案1】:

您错误地添加了目标

cell.fTCOne.tag = indexPath.section
cell.fTCOne.addTarget(self, action: #selector(oneSelected), for: .touchUpInside)

cell.fTCTen.tag = indexPath.section
cell.fTCTen.addTarget(self, action: #selector(tenSelected), for: .touchUpInside)

cell.fTCTwenty.tag = indexPath.section
cell.fTCTwenty.addTarget(self, action: #selector(twentySelected), for: .touchUpInside)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    相关资源
    最近更新 更多