How to detect touch event in table cells for iphone

how to detect touch event for table cells i tried this

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //<my stuff> [super touchesBegan:touches withEvent:event]; } 

but it doesn’t work fully, I have aUIimage view in the table cell and I want to use imgae based on the tap, so my touch event does not work for this cell

+6
iphone
source share
3 answers

If you want to detect a touch on a UITableViewCell, you do not need to detect touch events. In your subclass of UITableViewController, you need to implement the following delegation method:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 

Then you change the image of the table cell for the selected index path.

+9
source share

You probably need to set myImageView.userInteractionEnabled = YES; .

+2
source share

In one of my projects, I needed some kind of click on the tableView to dismiss the keyboard in order to display the underlying tableView. Since UITableView is indeed a UIScrollView, it will respond to scrollView delegate methods. Using these two methods will be rejected if the user clicks on a cell or scrolls through the tableView at all:

IMPORTANT: make sure you use UIScrollViewDelegate in the .h file, as well as UITableViewDelegate and UITableViewDataSourceDelegate !!!

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //remove keyboard if table row is clicked if ([self.firstName isFirstResponder] || [self.lastName isFirstResponder]) { [tableView deselectRowAtIndexPath:indexPath animated:NO]; [self.firstName resignFirstResponder]; [self.lastName resignFirstResponder]; } } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { //remove keyboard if table scrolls if ([self.firstName isFirstResponder] || [self.lastName isFirstResponder]) { [self.firstName resignFirstResponder]; [self.lastName resignFirstResponder]; } } 
+2
source share

All Articles