我 14 岁,住在德国,抱歉我的英语不好!
我想将 UIButtons 添加到我的 TableViewCells 中,当点击它们时,它们会调用自己的索引。
我使用 Swift 4。
最佳答案
首先创建 UIButton 到 tableviewCell 类的导出
现在在你的 viewController 中
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
即可获取您单击的按钮单元格索引
关于ios - 如何将 UIButtons 添加到我的 TableViewCell 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51014255/