How to get touch coordinates upon SELECTING a custom UITableViewCell?
Asked Answered
W

4

10

When I touch (Touch Up) a UITableViewCell my ViewController's UITableViewDelegate method - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath is called. I need to get the x-coordinate of the Touch-Point at this time as well so that I can know what portion of the Cell was touched (non-accessory items). I have tried using the usual touch methods such as TouchesDidEnd but the coordinates always return x=0.000 y=0.000 no matter where I touched (A location in a custom UITableViewCell in a UITableView object in my ViewController). I also tried implementing touch handling from within the Custom Cell class and while I COULD get accurate coordinates alas I could find no way to communicate those coordinates to my ViewController class (which has the UITableView).

Q: Is there a good way I can get the x-coordinates of the device's screen when I touch a custom UITableViewCell?

Whensoever answered 14/8, 2009 at 9:50 Comment(0)
U
5

Put a transparent UIView atop your table view, override its -touchesBegan:withEvent: etc. methods. In those overridden methods, get the UITouch objects and call -locationInView: on them to get the CGPoint values.

That gives you the coordinates of a touch event.

Then you just pass on those UITouch events to the underlying table view. The table view handles its usual business of passing touches on to rows, etc.

Unheard answered 31/12, 2009 at 7:54 Comment(1)
I'm doing so but in this way I'm preventing the uitableview to scroll, any suggestion?Audile
P
7

1. Add a tapGestureRecognizer to your tableView

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tableViewTapped(recognizer:)))
tableView.addGestureRecognizer(tapGestureRecognizer)

2. Write a function to handle the tapGesture

@objc func tableViewTapped(recognizer: UITapGestureRecognizer) {
    let location = recognizer.location(in: self.tableView) // point of touch in tableView
    if let indexPath = self.tableView.indexPathForRow(at: location) { // indexPath of touch location
        if let cell = tableView.cellForRow(at: indexPath) as? MyCustomCell {
            let locationInCell = recognizer.location(in: cell) // point of touch in cell
            // do something with location or locationInCell

            if cell.imageView.frame.contains(location) || cell.imageView.frame.contains(locationInCell) {
                    print("ImageView inside cell tapped!")        
            }
        }
    }
}
Polycarp answered 16/7, 2018 at 23:25 Comment(0)
U
5

Put a transparent UIView atop your table view, override its -touchesBegan:withEvent: etc. methods. In those overridden methods, get the UITouch objects and call -locationInView: on them to get the CGPoint values.

That gives you the coordinates of a touch event.

Then you just pass on those UITouch events to the underlying table view. The table view handles its usual business of passing touches on to rows, etc.

Unheard answered 31/12, 2009 at 7:54 Comment(1)
I'm doing so but in this way I'm preventing the uitableview to scroll, any suggestion?Audile
K
-1

What about:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let rect = tableView.rectForRow(at: indexPath as IndexPath)
    var point = CGPoint(x: rect.midX, y: rect.midY)
    point = tableView.convert(point, to: nil)
    print(point)
}
Karachi answered 4/11, 2015 at 16:17 Comment(5)
this would give you the rect of the cell and not the touch point.Boomerang
@Boomerang -convertPoint: return a CGPoint... what are you talking about ?Karachi
your code gets the mid point of the cell's rect. The question was about getting the touch coordinate in the view itself.Boomerang
Good ! the answer I was looking forAmyloid
You code getting points the user tapped on cells, but when user tapped on components in cells it's not getting pointsLength
R
-1

1) Pass view controller object in the custom cell

class ImageTableViewCell: UITableViewCell {

      @IBOutlet weak var img: UIImageView!

      var vc: UIViewController? = nil

      var delegate: TouchLocation?

}

2) Override the custom table view cell with touchesBegan method, and override super touchesBegan method

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    delegate?.touchLocation(location: (touch?.location(in: self.vc?.view))!)
    if let aTouches = touches as? Set<UITouch>, let anEvent = event {
        super.touchesBegan(aTouches, with: anEvent)
    }
}

3) Get location and pass to view controller using delegation

protocol TouchLocation {
   func touchLocation(location: CGPoint)
}

extension ImageListViewController: TouchLocation{

   func touchLocation(location: CGPoint) {
         self.touchedPosition = location // Here you will get touch location 
   }

}
Rootlet answered 10/8, 2018 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.