为什么在Swift中调用getIndex(cell:TableViewCell1)函数时出现错误?

时间:2020-08-01 14:54:58

标签: ios swift uitableview nsindexpath

我正在尝试在视图控制器中获取要为其使用此功能的单元格的indexpath

func getIndex(cell : TableViewCell1) -> IndexPath
    {
       let x = cell.cellTextField.convert(CGPoint.zero, to: self.tableView)
        let indexPath = self.tableView.indexPathForRow(at: x)
        return indexPath!
    
    }

它工作正常,但是当我尝试将此函数返回的值用于我的IBAction方法时:

  @IBAction func tapButton(_ sender: UIButton) {
let indexPath1 = getIndex(cell: TableViewCell1)
    }

我收到此错误

无法将类型“ TableViewCell1.Type”的值转换为预期的参数类型“ TableViewCell1”。

对此需要帮助。如果这是一个非常基本的问题,请原谅我,因为我迅速着手乞讨。

1 个答案:

答案 0 :(得分:0)

错误消息说该方法获取的对象是TableViewCell1的一种类型,但是您提供了类型定义。

如果方法tapButton在TableViewCell1的子类中,则

class TableViewCell1: UITableViewCell {
    @IBAction func tapButton(_ sender: UIButton) {
        let indexPath1 = getIndex(cell: self)
    }
}