【问题标题】:How to select a button in the tableview? [duplicate]如何在表格视图中选择一个按钮? [复制]
【发布时间】:2017-07-10 21:25:40
【问题描述】:

我可以在swift 3的tableview中使用“didSelectRowAt”函数来选择行。但是当我连续选择一个按钮时,我该如何做其他动作呢?我不知道如何在 tableview 中选择一个按钮。

【问题讨论】:

  • 您想要多选或单选按钮。
  • 我将使用多选按钮。
  • 您应该创建一个自定义的 uitableviewcell 并在其中添加按钮并在 tablecell 内创建一个委托,当点击按钮时,调用该委托方法

标签: ios uitableview swift3 tableview


【解决方案1】:

创建自定义单元格
在 UItableview 方法的cellForRowAt indexPath 内写下面的代码

cell.buttonname?.addTarget(self, action: #selector(self.buttonmethod), for: .touchUpInside)
cell.buttonname.tag = indexPath.section

//按钮点击方法

func buttonmethod(_ button: UIButton) {
     print(button.tag)
     // perform some action
}

【讨论】:

  • 这是 Swift 3 版本代码吗?就我而言,它不起作用。
【解决方案2】:

创建一个出口动作:并使用此代码找到索引路径

    @IBAction func memberPicTapped(_ sender:UIButton) {
            var superView = sender.superview
            while !(superView is UITableViewCell) {
                superView = superView?.superview
            }
            let cell = superView as! UITableViewCell
            if let indexpath = YourTableView.indexPath(for: cell){

            }
      }

【讨论】:

  • 非常感谢!成功了!
  • 没问题?特拉维斯
猜你喜欢
  • 2015-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-12
  • 2013-06-28
  • 1970-01-01
  • 2014-06-19
相关资源
最近更新 更多