在UITableView上触摸事件?

| 我在视图中有child0ѭ和
UITableView
作为孩子, 我要做的是,当我触摸任何一行时,我都会在底部显示一个视图。如果用户在行或bottomView以外的地方触摸任何其他内容,我想隐藏该视图。 问题是当我单击
UITableView
时不会触发
touchesEnded
事件。 现在如何检测detect1上的触摸并将其与行选择事件区分开。 谢谢。     
已邀请:
无需继承任何子类,您可以在
UITableView
上添加
UITapGestureRecognizer
并根据自己的条件来吸收或不吸收手势。 在您的viewDidLoad中:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapOnTableView:)];
[self.myTableView addGestureRecognizer:tap];
然后,针对条件执行以下操作:
-(void) didTapOnTableView:(UIGestureRecognizer*) recognizer {
    CGPoint tapLocation = [recognizer locationInView:self.myTableView];
    NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:tapLocation];

    if (indexPath) { //we are in a tableview cell, let the gesture be handled by the view
        recognizer.cancelsTouchesInView = NO;
    } else { // anywhere else, do what is needed for your case
        [self.navigationController popViewControllerAnimated:YES];
    }
}
请注意,如果您只是想简单地点击表格中任意位置的点击,而不要点击单元格行中任何按钮的点击,则只需要使用上面的第一个代码片段。一个典型的示例是当您有一个UITableView且还有一个UISearchBar时。当用户单击,滚动等表格视图时,您要消除搜索栏。代码示例...
-(void)viewDidLoad {
    [super viewDidLoad];
    etc ...

    [self _prepareTable];
}
-(void)_prepareTable {
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.allowsSelection = NO;
    etc...

    UITapGestureRecognizer *anyTouch =
        [[UITapGestureRecognizer alloc]
         initWithTarget:self action:@selector(tableTap)];
    [self.tableView addGestureRecognizer:anyTouch];
}
// Always drop the keyboard when the user taps on the table:
// This will correctly NOT affect any buttons in cell rows:
-(void)tableTap {
    [self.searchBar resignFirstResponder];
}
// You probably also want to drop the keyboard when the user is
// scrolling around looking at the table. If so:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.searchBar resignFirstResponder];
}
// Finally you may or may not want to drop the keyboard when
// a button in one cell row is clicked. If so:
-(void)clickedSomeCellButton... {
    [self.searchBar resignFirstResponder];
    ...
}
希望它可以帮助某人。     
您应该将touch事件转发到视图的控制器。 子类化tableview控件,然后重写该方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];  //let the tableview handle cell selection 
    [self.nextResponder touchesBegan:touches withEvent:event]; // give the controller a chance for handling touch events 
}
然后,您可以在控制器的触摸方法中执行所需的操作。     
我只是偶然发现了可能解决您的问题的方法。创建表视图时使用以下代码:
tableView.canCancelContentTouches = NO;
如果不将其设置为
NO
,则在表视图中甚至有一点垂直移动时,触摸事件就会被取消(如果将NSLog语句放入代码中,则您会看到
touchesCancelled
在表格开始垂直滚动)。     
很长一段时间以来我一直在面对这个问题,却没有任何有效的解决方案。最后,我选择了一种替代方法。从技术上来说,我知道这不是解决方案,但这可以帮助确定的人。 在我的情况下,我想选择一个将显示某些选项的行,然后触摸表或视图上的任意位置,我想隐藏这些选项或执行任何任务,但之前为该行选择的行除外: 设置视图的触摸事件。当您触摸除表视图之外的其他任何视图时,将执行此任务。 TableView的didSelectRowAtIndexPath执行以下操作
 - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    if(indexPath.row != previousSelectedRow && previousSelectedRow != -1) {
        // hide options or whatever you want to do
        previousSelectedRow = -1;
    }
    else {
        // show your options or other things
        previousSelectedRow = indexPath.row;
    }
 }
我知道这是较旧的帖子,不是很好的技术解决方案,但这对我有用。我正在发布此答案,因为这可能肯定会对某人有所帮助。 注意:由于在此处直接键入代码,因此此处编写的代码可能会有拼写错误。 :)     
试试这个方法:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{

}
使用scrollViewDidEndDragging之类的touchesEnded替代方法。希望能帮助到你。     
要在ѭ1上接收触摸事件,请使用:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  //<my stuff>

  [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   //<my stuff>

   [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
  //<my stuff>

  [super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
{
   //<my stuff>
   [super touchesCancelled:touches withEvent:event];
}
在UITableView中接收触摸事件     
在控制器类中,声明一个删除底视图的方法。像这样:
-(IBAction)bottomViewRemove:(id)sender {

[bottomView removeFromSuperview];

}
在Interface Builder中,选择视图,然后在“自定义类”部分的身份检查器中,将类从“ 19”更改为“ 20”。之后,转到连接检查器,并将TouchUpInside事件连接到上面声明的方法。希望这可以帮助。     

要回复问题请先登录注册