2017-09-23 50 views
0

我需要检测,如果按钮被点击的的UITableViewController问题检测按钮cellForRowAt

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

let LikesBtn = cell.viewWithTag(7) as! UIButton 

} 

回答

4

Swift中最简单和最有效的方式是回调闭包。

  • 子类UITableViewCell,该viewWithTag的方式来识别UI元素已经过时。
  • 将自定义单元格的类设置为子类的名称,并在Interface Builder中将标识符设置为ButtonCellIdentifier

  • 添加callback属性。

  • 添加动作并将按钮连接到动作。

    class ButtonCell: UITableViewCell { 
    
        var callback : (()->())? 
    
        @IBAction func buttonPressed(_ sender : UIButton) { 
         callback?() 
        } 
    } 
    
  • cellForRow分配回调到自定义单元格。

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
        let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonCellIdentifier", for: indexPath) as! ButtonCell 
        cell.callback = { 
         print("Button pressed", indexPath) 
        } 
        return cell 
        } 
    
  • 当按下按钮时,调用回调。索引路径被捕获。

+0

很酷。我第一次看到这个。 – Shades

+0

它工作完美,谢谢 –

0

第一步: 让子类为您的自定义的UITableViewCell,还注册协议。

事情是这样的:

protocol MyTableViewCellDelegate: class { 
    func onButtonPressed(_ sender: UIButton, indexPath: IndexPath) 
} 

class MyTableViewCell: UITableViewCell { 

    @IBOutlet var cellButton: UIButton! 

    var cellIndexPath: IndexPath! 
    var delegate: MyTableViewCellDelegate! 


    override func awakeFromNib() { 
     super.awakeFromNib() 

     cellButton.addTarget(self, action: #selector(self.onButton(_:)), for: .touchUpInside) 
    } 

    func onButton(_ sender: UIButton) { 
     delegate.onButtonPressed(sender, indexPath: cellIndexPath) 
    } 

} 

在你TableViewController,确保它符合你刚刚创建协议 “MyTableViewCellDelegate”。

请看下面的代码以获得更好的理解。

class MyTableViewController: UITableViewController, MyTableViewCellDelegate { 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     if let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as? MyTableViewCell { 

      cell.cellIndexPath = indexPath 
      cell.delegate = self 

      return cell 
     } else { 
      print("Something wrong. Check your cell idetifier or cell subclass") 
      return UITableViewCell() 
     } 
    } 

    func onButtonPressed(_ sender: UIButton, indexPath: IndexPath) { 
     print("DID PRESSED BUTTON WITH TAG = \(sender.tag) AT INDEX PATH = \(indexPath)") 
    } 
} 
2

下面是我用:

首先初始化按钮为OutletactionTableViewCell

class MainViewCell: UITableViewCell { 

@IBOutlet weak var testButton: UIButton! 

@IBAction func testBClicked(_ sender: UIButton) { 

    let tag = sender.tag //with this you can get which button was clicked 

} 



} 

然后在你主控制器在cellForRow函数只是初始化标签的按钮是这样的:

class MainController: UIViewController, UITableViewDelegate, UITableViewDataSource, { 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MainViewCell 


     cell.testButton.tag = indexPath.row   

     return cell 
    } 

} 
+0

不能同意更多,我也在我的项目中使用它,像魅力 –

+0

如果表视图支持删除,插入或移动的能力,请不要使用索引路径作为标记行。 – rmaddy

+0

@rmaddy如果表视图可以插入删除或移动行,那么它会重新加载自己,所以标签将再次初始化,并且按钮将具有正确的索引他们不? (我没有检查它,但我相信它不会有问题) –