问题检测按钮cellForRowAt

时间:2017-09-23 10:19:43

标签: ios swift uitableview

我需要检测是否在UITableViewController中单击了按钮

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

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

}

3 个答案:

答案 0 :(得分:9)

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
      }
    
  • 按下按钮时,将调用回调。捕获索引路径。

修改

如果可以添加或删除单元格,则需要注意。在这种情况下,通过从数据源数组

获取当前索引来更新索引路径
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonCellIdentifier", for: indexPath) as! ButtonCell
     let item = dataSourceArray[indexPath.row]
     // do something with item
     cell.callback = {
         let actualIndexPath = IndexPath(row: dataSourceArray.index(of: item)!, section: indexPath.section)
         print("Button pressed", actualIndexPath)  
     }
     return cell
  }

如果即使section可以改变,那么协议/代理可能会更有效。

答案 1 :(得分:1)

以下是我使用的内容:

首先将按钮初始化为Outletaction上的TableViewCell

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
    }

}

答案 2 :(得分:0)

第一步: 为您的自定义UITableViewCell创建子类,也注册协议。

这样的事情:

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

class MyTableViewCell: UITableViewCell {

    @IBOutlet var cellButton: UIButton!

    var cellIndexPath: IndexPath!
    weak 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)")
    }
}