当按钮接收事件时,UITapGestureRecognizer到cellForRowAt

时间:2018-01-11 04:09:13

标签: ios swift uitableview uitapgesturerecognizer

我是Swift的新手学习者。我被困在这里。我希望当我点击按钮,单元格更改并显示警告但我觉得我错了。请解释一下。当我提出问题时,我会考虑很多。

以下是代码:

@objc func handleTap(_ sender: UITapGestureRecognizer) {
    ManagerProfileTableView.reloadData()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "managerProfileCell") as! ManagerInformationTableCell
    let cellButton = tableView.dequeueReusableCell(withIdentifier: "buttonUpdateProfile") as! UpdateProfileTableCell

    if indexPath.row == titlesInformationArr.count - 1 {
        cellButton.updateButton.setTitle("Update", for: UIControlState.normal)
        cellButton.updateButton.layer.cornerRadius = 5
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_: )))
        cellButton.updateButton.isUserInteractionEnabled = true
        cellButton.updateButton.addGestureRecognizer(tap)
        if cell.informationTextField != nil {
            cell.warningImage.isHidden = true
        } else {
            cell.warningImage.isHidden = false
        }
        return cellButton

    } else {
        cell.informationTextField.text = titlesInformationArr[indexPath.row].value
        cell.titlesLabel.text = titlesInformationArr[indexPath.row].title
        return cell
    }

3 个答案:

答案 0 :(得分:0)

您的cellButton可能不应该是可重复使用的单元格 - cell是什么 - 而是您添加到出列的可重复使用单元格中的UIButton。如果你这样添加它,你必须考虑细胞的生命周期。因为它是可重复使用的,所以冒着向单元格添加按钮的风险,即使它之前已经使用过并且已经有一个按钮...除非您正在执行相反操作,并删除按钮,prepareForReuse()

(当你向上和向下滚动时,当一个单元格从屏幕上移开时,它会返回到一个可供重复使用的单元格池中,并且可以在它消失的状态下返回给你,就像你调用{ {1}}。)

在这种情况下,您可能最好在界面构建器中设计原型单元并在其中添加按钮,然后继承UITableViewCell并在该子类中为按钮事件dequeueReusableCell创建IBAction。当收到该操作时,单元格可以执行一些操作,其中可能包括调用委托方法(其中持有表视图的视图控制器是委托),以执行与示例中的touchUpInside表视图相关的操作。

如果您只想在任何地方检测单元格中的点击,单元格就可以这样做 - 只需确保连接UITableViewDelegate - 通常连接到实现UITableViewDataSource的同一位置 - 并实现方法{{1 }}。您应该能够在此站点上找到大量使用此方法的示例。

如果您想知道您看到的警告可能意味着您可以编辑答案并将其包括在内吗?

答案 1 :(得分:0)

Hello @Lucifer您不需要添加UITapGestureRecognizer,因为UITableView有自己的委托,您只需附加tableview 右键单击tableview并将其放在ViewController

  

参见下面的图片

enter image description here

  

然后在ViewController类中调用此委托方法,它将在单元格选择

上调用

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

// do what you want to do here 

}

答案 2 :(得分:0)

如果你想控制单元格,你不需要在单元格上使用tapGesture。你可以简单地在按钮上添加一个标签然后使用标签来获取该单元格。这是你修改过的代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "managerProfileCell") as! ManagerInformationTableCell
    let cellButton = tableView.dequeueReusableCell(withIdentifier: "buttonUpdateProfile") as! UpdateProfileTableCell

    if indexPath.row == titlesInformationArr.count - 1 {
        cellButton.updateButton.setTitle("Update", for: UIControlState.normal)
        cellButton.updateButton.layer.cornerRadius = 5
        cellButton.tag = indexPath.row
        cellButton.addTarget(self , action : #selector(cellButtonTapped(_:)) , for : .touchUpInside)

        cellButton.updateButton.isUserInteractionEnabled = true

        if cell.informationTextField != nil {
            cell.warningImage.isHidden = true
        } else {
            cell.warningImage.isHidden = false
        }
        return cellButton

    } else {
        cell.informationTextField.text = titlesInformationArr[indexPath.row].value
        cell.titlesLabel.text = titlesInformationArr[indexPath.row].title
        return cell
    }

@objC func cellButtonTapped(_ sender : UIButton){
  let index = sender.tag
  let cellToChange = tableView.cellForRowAt(indexPath : [0,index]) as! ManagerInformationTableViewCell
  cellToChange.backgroundColor = .green

}