Here is my code.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CellAvailableJobs = self.tblJobs.dequeueReusableCell(withIdentifier: "CellAvailableJobs") as! CellAvailableJobs
let jobsNearByObj:JobsNearBy = self.jobsArray![indexPath.row]
cell.loadCell(jobsNearByObj: jobsNearByObj)
cell.btnHeart.addTarget(self, action: #selector(JobsVC.btnBookmarkAction), for: .touchUpInside)
cell.btnHeart.tag = indexPath.row
return cell
}
@IBAction func btnBookmarkAction(_ sender: AnyObject){
let button = sender as? UIButton
print("tag: \(String(describing: button?.tag))")
let cell = button?.superview?.superview as? UITableViewCell
let indexPath = tblJobs.indexPath(for: cell!)
print("bookmarkedJobId: \(String(describing: indexPath.row))")
}
I'm fetching data from web and populating them on tableview, everything is working well. I want to print indexpath.row when the button on cell is tapped. On above method the compiler is complaining that the cell is nil. This line: let indexPath = tblJobs.indexPath(for: cell!).
What is the problem with this code. How the cell is nil in this code. Can anyone help me with this issue?