Sync
Sync

Reputation: 135

Receive TouchesBegan of UITableViewCell in UIViewController

How can I receive TouchesBegan from a UITableViewCell in a UITableViewController so I can know when the user touches some cell?

Upvotes: 0

Views: 4943

Answers (2)

Jesse Rusak
Jesse Rusak

Reputation: 57168

When you touch the a cell, it becomes highlighted; try overriding setHighlighted: in a subclass of UITableViewCell to adjust your appearance when it changes to YES (user touching) or NO (user lifted off).

(Overriding touchesBegan:withEvent: will also work, but it's easier to use setHighlighted: in order to capture when you're not longer touching; otherwise you need to override touchesEnded:withEvent: and touchesCancelled:withEvent:).

Upvotes: 3

Dan F
Dan F

Reputation: 17732

If you're looking for an event that is triggered when a user taps a cell, but before it is officially selected, there is the UITableViewDelegate method:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

From the documentation:

This method is not called until users touch a row and then lift their finger; the row isn't selected until then, although it is highlighted on touch-down. You can use UITableViewCellSelectionStyleNone to disable the appearance of the cell highlight on touch-down. This method isn’t called when the table view is in editing mode (that is, the editing property of the table view is set to YES) unless the table view allows selection during editing (that is, the allowsSelectionDuringEditing property of the table view is set to YES).

EDIT

In order to achieve the behavior you're seeking, I believe you need to subclass UITableViewCell and implement the method

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

You can find more about the event handling in the UIResponder class reference, which all UIViews subclass from.

Upvotes: 1

Related Questions