【问题标题】:How can I add UIButtons to my TableViewCell? [duplicate]如何将 UIButtons 添加到我的 TableViewCell? [复制]
【发布时间】:2018-06-24 21:46:45
【问题描述】:

我今年 14 岁,住在德国,抱歉我的英语不好!

我想将 UIButtons 添加到我的 TableViewCells 中,当它们被点击时调用它们自己的索引。

我使用 Swift 4。

【问题讨论】:

  • 到目前为止你有什么尝试?

标签: ios swift uitableview uibutton cell


【解决方案1】:

首先将您的 UIButton 创建到您的 tableviewCell 类

现在在您的视图控制器中

   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellIdentifier", for: indexPath) as! YourTableViewCell
       cell.bttnName.tag = indexPath.row // The indexPath Row will be stored an button's tag
        cell.bttnName.addTarget(self, action: #selector(selectedButton), for: .touchUpInside)


        return cell
    }

现在为按钮创建一个目标函数(按钮动作)

 @objc func selectedButton(sender: UIButton!) {
        let button = sender!
        print("Clicked Index:",button.tag)
}

只需调用button.tag即可获取您点击的按钮单元格索引

【讨论】:

  • 对我不起作用:(
  • @Thomas 请出示您的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
  • 2017-10-02
  • 2019-07-16
  • 2010-11-06
相关资源
最近更新 更多