[英]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 但是它不能正常工作我在表格单元格中有一个iUIimage视图,我想基于tap点击imgon,所以我的触摸事件不适用于那个单元格
If you want to detect a touch on the UITableViewCell you don't really need to detect touch events. 如果要检测UITableViewCell上的触摸,则不需要检测触摸事件。 In your UITableViewController subclass, you need to implement the following delegate method: 在UITableViewController子类中,您需要实现以下委托方法:
- (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;
您可能需要设置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. 在我的一个项目中,我需要点击tableView来关闭键盘,以便底层的tableView显示。 Since a UITableView is really a UIScrollView, it will respond to the scrollView delegate methods. 由于UITableView实际上是一个UIScrollView,它将响应scrollView委托方法。 Using these 2 methods will dismiss if either the user taps on a cell or scrolls the tableView at all: 如果用户点击一个单元格或者根本滚动tableView,则使用这两种方法将被忽略:
IMPORTANT: Make sure you implement the UIScrollViewDelegate in your .h file as well as the UITableViewDelegate and UITableViewDataSourceDelegate!!! 重要提示:确保在.h文件中实现UIScrollViewDelegate以及UITableViewDelegate和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];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.