I would like to implement feature that if profileImage tapped in tableviewCell, segue to detailView. I add tapGesture but I still can\'t figure out how to get indexPath to p
I have made a sample project to demonstrate the solution. It is working perfectly fine. See the gif!
You can download the sample project code using the link https://drive.google.com/file/d/1zF8Uq2H6eQYyrHU6KqnZu7b5cU_HtZY0/view?usp=sharing
Actually, you have to add a gesture recognizer to your image and attach an action to this gesture. See the implementation of cellforRowAt.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"TableViewCellUser") as! TableViewCellUser
//handle image and its gesture
cell.imgUser.image = userImagesArray[indexPath.row]
//enable the interaction so that touch could be captured.
cell.imgUser.isUserInteractionEnabled = true
let gesture = UITapGestureRecognizer(target: self, action: #selector(userImageTapped(gesture:)))
gesture.numberOfTouchesRequired = 1
gesture.delegate = self as? UIGestureRecognizerDelegate
cell.imgUser.tag = indexPath.row
cell.imgUser.addGestureRecognizer(gesture)
//handle label
cell.lblUserName.text = userNames[indexPath.row]
return cell
}
//action on user tap
guard let indexPathRow = gesture.view?.tag else {
return
}
print(indexPathRow)
guard indexPathRow >= 0 else {
print("Array index must be greater than zero. Going to return")
return
}
selectedUserImage = userImagesArray[indexPathRow]
selectedUserName = userNames[indexPathRow]
self.performSegue(withIdentifier: "UsersToDetail", sender: self)
You can download the fully working code from the above mentiond link. Let me know in comments if any problem Occurs
This link will also help [UITapGestureRecognizer tag]: unrecognized selector sent to instance