Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect touch event in table cells for iphone

Tags:

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 its not working actuallly i have aUIimage view in table cell and i want to chnage imgae based on tap so my touch event is not working for that cell

like image 315
ram Avatar asked Jul 28 '10 03:07

ram


3 Answers

If you want to detect a touch on the UITableViewCell you don't really need to detect touch events. In your UITableViewController subclass, you need to implement the following delegate method:

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

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

like image 174
jergason Avatar answered Nov 15 '22 10:11

jergason


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

like image 23
jtbandes Avatar answered Nov 15 '22 08:11

jtbandes


In one of my projects I needed any tap on the tableView to dismiss the keyboard so the underlying tableView would show. Since a UITableView is really a UIScrollView, it will respond to the scrollView delegate methods. Using these 2 methods will dismiss if either the user taps on a cell or scrolls the tableView at all:

IMPORTANT: Make sure you implement the UIScrollViewDelegate in your .h file as well as the 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]; 
    }
}
like image 33
DoctorG Avatar answered Nov 15 '22 10:11

DoctorG