确定按下了哪个表格视图单元格按钮?
我有像测验这样的表视图单元格。 在每个单元格中我都有一个按钮如何识别按下哪个单元格按钮。 也许通过IndexPath ??? 这是我连接按钮的方式
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionCell")! variant1 = cell.contentView.viewWithTag(1) as! UIButton variant2 = cell.contentView.viewWithTag(2) as! UIButton variant3 = cell.contentView.viewWithTag(3) as! UIButton variant4 = cell.contentView.viewWithTag(4) as! UIButton variant1.addTarget(self, action: #selector(self.variant1ButtonPressed), for: .touchUpInside) variant2.addTarget(self, action: #selector(self.variant2ButtonPressed), for: .touchUpInside) variant3.addTarget(self, action: #selector(self.variant3ButtonPressed), for: .touchUpInside) variant4.addTarget(self, action: #selector(self.variant4ButtonPressed), for: .touchUpInside) return cell } func variant1ButtonPressed() { print("Variant1") variant1.backgroundColor = UIColor.green } func variant2ButtonPressed() { print("Variant2") variant2.backgroundColor = UIColor.green } func variant3ButtonPressed() { print("Variant3") variant3.backgroundColor = UIColor.green } func variant4ButtonPressed() { print("Variant4") variant4.backgroundColor = UIColor.green }
这是它在Storyboard中的样子:
使用此行获取indexPath,您必须在目标选择器上传递UIButton
func buttonTapped(_ sender:AnyObject) { let buttonPosition:CGPoint = sender.convert(CGPointZero, to:self.tableView) let indexPath = self.tableView.indexPathForRow(at: buttonPosition) }
您应该使用委托模式,基本示例:
protocol MyCellDelegate { func didTapButtonInside(cell: MyCell) } class MyCell: UITableViewCell { weak var delegate: MyCellDelegate? func buttonTapAction() { delegate?.didTapButtonInside(cell: self) } } class ViewController: MyCellDelegate { let tableView: UITableView func didTapButtonInside(cell: MyCell) { if let indexPath = tableView.indexPath(for: cell) { print("User did tap cell with index: \(indexPath.row)") } } }
由于操作需要在视图控制器内,ctrl +从按钮拖动到视图控制器 – 这将使用响应器链 。
基本上你需要将视图(按钮)转换为表视图的坐标系,以便告诉IndexPath是什么,如果你有IndexPath,你有一个对应于被点击的单元格内的按钮的对象:
@IBAction func buttonTapped(_ sender: Any) { if let indexPath = getIndexPath(of: sender) { // Your implementation... } } private func getIndexPath(of element:Any) -> IndexPath? { if let view = element as? UIView { // Converting to table view coordinate system let pos = view.convert(CGPoint.zero, to: self.tableView) // Getting the index path according to the converted position return tableView.indexPathForRow(at: pos) as? IndexPath } return nil }
值得一提的是,您的问题有很多解决方案。 但是你应该知道,在Apple的示例项目中,他们也使用这种技术。
这是你在UITableView中向UIButton添加标签的方法,在下面添加代码行
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell cell.yourButton.tag = indexPath.row cell.yourButton.addTarget(self, action:#selector(btnPressed(sender:)), for: .touchUpInside)
在ViewController中添加此function
func btnPressed(sender: UIButton) { print("Button tag \(sender.tag)") }
希望这可以帮助…
Simple Subclass按钮就像JSIndexButton一样
class JSIndexButton : UIButton { var indexPath : IndexPath! }
现在在cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ItemCell let itemCategory = dataList[button.indexPath.section]; let item = itemCategory.items[button.indexPath.row]; cell.imgView.setImageWithURL(item.photoUrl); cell.btnBuy.indexPath = indexPath; cell.btnBuy.addTarget(self, action: #selector(JSCollapsableTableView.btnBuyPressed(_:)), for: UIControlEvents.touchUpInside) return cell; }
检查按钮操作
@IBAction func btnBuyPressed(_ button: JSIndexButton) { let itemCategory = dataList[button.indexPath.section]; let item = itemCategory.items[button.indexPath.row]; }
- Swift — 4 —核心数据—第3部分创建Singleton Core Data和重构插入,更新,删除操作
- 在不同的UITableViewControllers中的UITableViewCells上一致使用UIImageView
- SKLightNode性能问题
- 在iOS扩展程序和包含钥匙串的应用程序之间共享?
- 为什么“didset”在设置属性的属性时会调用属性?
- CGContext:如何擦除位图上下文之外的像素(例如kCGBlendModeClear)?
- 获取UITableView来填充来自另一个类的数据
- 如何在2019年购买iOS App简短评论
- iTunes Connect:带有特殊字符的应用程序名称(德语’umlaute’)