Getting UITableViewCell from UITapGestureRecognizer

I have a UITableView with several sections, and in my cellForRowAtIndexPath method I add a UITapGestureRecognizer to the imageView of the cell (each cell has a small image). I managed to access this image (to change it) using:

var imageView : UIImageView! = sender.view! as UIImageView 

However, I need to make access to the cell data, which, I believe, means that I need to have access to the cell in order to get the section number and line number. Can anyone advise how to do this? The idea is that I change the image to a checkmark without a mark if the task is completed, but then in my data I need to mark this task as completed.

+6
source share
3 answers

In your function that processes the click gesture recognizer, use the tableView indexPathForRowAtPoint function to get an additional index path for your touch event, for example:

 func handleTap(sender: UITapGestureRecognizer) { let touch = sender.locationInView(tableView) if let indexPath = tableView.indexPathForRowAtPoint(touch) { // Access the image or the cell at this index path } } 

Here you can call cellForRowAtIndexPath to get the cell or any of its contents, since now you have a pointer path to the selected cell.

+17
source

You can get the position of the displayed image to find indexPath, and from there find the cell that has this image:

 var position: CGPoint = sender.locationInView(self.tableView) var indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(position)! var cell = self.tableView(tableView, cellForRowAtIndexPath: indexPath) 
+4
source

You are missing the fact that you can use the delegate design template here.

Create a protocol that informs delegates about the state change in your checkbox (imageView):

 enum CheckboxState: Int { case .Checked = 1 case .Unchecked = 0 } protocol MyTableViewCellDelegate { func tableViewCell(tableViewCell: MyTableViewCell, didChangeCheckboxToState state: CheckboxState) } 

And if you have a subclass of UITableViewCell :

 class MyTableViewCell: UITableViewCell { var delegate: MyTableViewCellDelegate? func viewDidLoad() { // Other setup here... let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "didTapImage:") imageView.addGestureRecognizer(tapGestureRecognizer) imageView.userInteractionEnabled = true } func didTapImage(sender: AnyObject) { // Set checked/unchecked state here var newState: CheckboxState = .Checked // depends on whether the image shows checked or unchecked // You pass in self as the tableViewCell so we can access it in the delegate method delegate?.tableViewCell(self, didChangeCheckboxToState: newState) } } 

And in a subclass of UITableViewController :

 class MyTableViewController: UITableViewController, MyTableViewCellDelegate { // Other methods here (viewDidLoad, viewDidAppear, etc)... override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) { var cell = tableView.dequeueReusableCellWithIdentifier("YourIdentifier", forIndexPath: indexPath) as MyTableViewCell // Other cell setup here... // Assign this view controller as our MyTableViewCellDelegate cell.delegate = self return cell } override func tableViewCell(tableViewCell: MyTableViewCell, didChangeCheckboxToState state: CheckboxState) { // You can now access your table view cell here as tableViewCell } } 

Hope this helps!

+2
source

Source: https://habr.com/ru/post/984474/


All Articles