【发布时间】:2018-06-24 21:46:45
【问题描述】:
我今年 14 岁,住在德国,抱歉我的英语不好!
我想将 UIButtons 添加到我的 TableViewCells 中,当它们被点击时调用它们自己的索引。
我使用 Swift 4。
【问题讨论】:
-
到目前为止你有什么尝试?
标签: ios swift uitableview uibutton cell
我今年 14 岁,住在德国,抱歉我的英语不好!
我想将 UIButtons 添加到我的 TableViewCells 中,当它们被点击时调用它们自己的索引。
我使用 Swift 4。
【问题讨论】:
标签: ios swift uitableview uibutton cell
首先将您的 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即可获取您点击的按钮单元格索引
【讨论】: