Why am I getting an error on calling getIndex(cell: TableViewCell1) function in swift?

42 Views Asked by At

I am trying to get the indexpath of a cell for which I am using this function in my view controller.

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

It works fine, but when I try to use the value returned by this function into my IBAction method:

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

I am getting this error

Cannot convert value of type 'TableViewCell1.Type' to expected argument type 'TableViewCell1'.

Need help regarding this. Pardon me if it is very basic question as I am begginer in swift.

1

There are 1 best solutions below

1
Blazej SLEBODA On

The error message says that method gets an object which is a type of TableViewCell1 but you give instead a type definition.

If the method tapButton is inside of subclass of TableViewCell1 then

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