简体   繁体   中英

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 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

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.

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

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]; 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM