单击 UITableView 中的标签时,tableview 单元格中的 IndexPath 返回错误

IndexPath in tablew view cell returned is wrong on click of a label in a UITableView

我在 table 视图单元格中有一个标签。

点击标签后,我想在检索到正确的 indexPath.

后转到另一个视图控制器

我在标签中添加了 gestureRecognizer。单击标签 return 是错误的 indexPath ,它不会 return 标签所在单元格的索引路径。

我该如何解决这个问题。任何帮助将不胜感激。谢谢

以下是我的代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        cell = tableView.dequeueReusableCell(withIdentifier: "ViewControllerCell", for: indexPath) as! TableCell

        cell.name.text = feeds[indexPath.row].name

        nameClicked()
        return cell
    }


 func nameClicked(){
        cell.name.isUserInteractionEnabled = true
        let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(TrendViewController.handleTap(gestureRecognizer:)))
        cell.name.addGestureRecognizer(gestureRecognizer)

    }


    func handleTap(gestureRecognizer: UIGestureRecognizer) {

        var touchPoint = cell.name.convert(CGPoint.zero, to: self.tableView)
        var clickedLabelIndexPath = tableView.indexPathForRow(at: touchPoint)!

        nameFromView = feeds[clickedLabelIndexPath.row].name
        print("IndexPath at touch",clickedLabelIndexPath)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "profile") as! ProfileViewController

        vc.clickedLabelIndexPath = clickedLabelIndexPath
        vc.nameFromView = feeds[clickedLabelIndexPath.row].name

        self.navigationController?.pushViewController(vc, animated: true)
    }

您已在 class 中将 cell 声明为实例 属性,因此您正在向同一单元格的标签添加手势。因此,使用 dequeueReusableCell 创建新单元格,并通过添加类型单元格的参数更改方法 nameClicked

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //create new cell
    let cell = tableView.dequeueReusableCell(withIdentifier: "ViewControllerCell", for: indexPath) as! TableCell
    cell.name.text = feeds[indexPath.row].name
    nameClicked(cell)
    return cell
}

func nameClicked(_ cell: TableCell){
    cell.name.isUserInteractionEnabled = true
    let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(TrendViewController.handleTap(gestureRecognizer:)))
    cell.name.addGestureRecognizer(gestureRecognizer)
}

现在像这样更改您的 handleTap 方法以获得正确的 indexPath

func handleTap(_ gestureRecognizer: UIGestureRecognizer) {
    let point = self.tableView.convert(CGPoint.zero, from: gestureRecognizer.view!)
    if let indexPath = self. tableView.indexPathForRow(at: point) {
        print("\(indexPath.section) \(indexPath.row)")
        //Add your code here
    }
}

注意:如果你的单元格只有一个单元格,那么如果你使用didSelectRowAt方法而不是向标签添加手势,那就更好了。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(indexPath.row)
}

也许你应该为检查单元格是否被触摸设置条件?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCell(withIdentifier: "ViewControllerCell", for: indexPath) as! TableCell
   if cell.isTouched { 
      cell.setTouched
   }
   else { 
      cell.setNoTouched
   }

   return cell
}