确定在哪个表视图中按下单元格按钮?

时间:2017-07-13 07:02:06

标签: ios swift uitableview

我有像测验这样的表格视图。在每个单元格中我都有一个按钮如何识别按下哪个单元格按钮。也许通过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中的样子:enter image description here

6 个答案:

答案 0 :(得分:4)

您应该使用委托模式,基本示例:

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)")
        }
    }    
}

答案 1 :(得分:3)

使用此行获取indexPath,您必须在目标选择器

上传递UIButton
func buttonTapped(_ sender:AnyObject) {
    let buttonPosition:CGPoint = sender.convert(CGPointZero, to:self.tableView)
    let indexPath = self.tableView.indexPathForRow(at: buttonPosition)
}

答案 2 :(得分:2)

由于操作需要在视图控制器内,ctrl +从按钮拖动到视图控制器 - 这将使用responder chain

基本上你需要将视图(按钮)转换为表视图的坐标系,以便知道什么是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的示例项目中,他们也使用这种技术。

答案 3 :(得分:0)

这是你在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中添加此功能

func btnPressed(sender: UIButton)
{
   print("Button tag \(sender.tag)")
}

希望这会有所帮助......

答案 4 :(得分:0)

简单子类按钮就像 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];

}

答案 5 :(得分:0)

 @objc func ItemsDescription(_ sender: UIButton?,event: AnyObject?) {
    let touches: Set<UITouch>
    touches = (event?.allTouches!)!
    let touch:UITouch = (touches.first)!
    let touchPosition:CGPoint = touch.location(in: self.tableView)
    let indexPath:NSIndexPath = self.tableView.indexPathForRow(at: touchPosition)! as NSIndexPath
}
  

添加目标

cell.ItemsDescription.addTarget(self, action: #selector(ItemsDescription(_:event:)), for: UIControlEvents.touchUpInside)