UITableView 上的触摸事件?

发布于 2024-11-04 12:53:42 字数 286 浏览 1 评论 0原文

我有 UIViewControllerUITableView 作为视图中的子项, 我想要做的是,当我触摸任何行时,我会在底部显示一个视图。如果用户触摸行或底部视图之外的任何其他位置,我想隐藏该视图。

问题是当我单击 UITableView 时,它不会触发 touchesEnded 事件。

现在我如何检测 UITableView 上的触摸并将其与行选择事件区分开来。

谢谢。

I have UIViewControllerand UITableView as child in the view,
what I want to do is when I touch any row I am displaying a view at bottom. I want to hide that view if the user touch any where else then rows or the bottomView.

The problem is when I click on UITableView it doesn't fires touchesEnded event.

Now how can I detect touch on UITableView and distinguish it with row selection event.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

缺⑴份安定 2024-11-11 12:53:43

尝试以下方法:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{

}

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

}

使用scrollViewDidEndDragging 替代touchesEnded。希望有帮助。

Try this methods:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{

}

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

}

Use scrollViewDidEndDragging like alternative of touchesEnded. Hope it helps.

旧瑾黎汐 2024-11-11 12:53:43

要接收 UITableView 上的触摸事件,请使用:

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

To receive touch events on the UITableView use:

- (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];
}
动听の歌 2024-11-11 12:53:43

在您的控制器类中声明一个删除底部视图的方法。像这样的事情:

-(IBAction)bottomViewRemove:(id)sender {

[bottomView removeFromSuperview];

}

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

In your controller class declare a method which removes the bottom view. Something like this:

-(IBAction)bottomViewRemove:(id)sender {

[bottomView removeFromSuperview];

}

In Interface Builder, select your view and in the identity inspector in the custom class section, change the class from UIView to UIControl. After that go to the connections inspector and connect the TouchUpInside event to the method declared above. Hope this helps.

深府石板幽径 2024-11-11 12:53:42

无需子类化任何内容,您可以将 UITapGestureRecognizer 添加到 UITableView 并根据您的标准吸收或不吸收手势。

在您的 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];
    ...
}

希望它对某人有帮助。

No need to subclass anything, you can add a UITapGestureRecognizer to the UITableView and absorb the gesture or not depending on your criteria.

In your viewDidLoad:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapOnTableView:)];
[self.myTableView addGestureRecognizer:tap];

Then, implement your action like this for the criteria:

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

And note that if you just want to simply pick up clicks anywhere on the table, but not on any buttons in cell rows, you need only use the first code fragment above. A typical example is when you have a UITableView and there is also a UISearchBar. You want to eliminate the search bar when the user clicks, scrolls, etc the table view. Code example...

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

Hope it helps someone.

謸气贵蔟 2024-11-11 12:53:42

您应该将触摸事件转发到视图的控制器。
子类化您的 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 
}

然后,您可以在控制器的触摸方法中执行您想要的操作。

You should forward the touch event to the view's controller.
Subclass your tableview control and then override the method:

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

then , you can do what you want in the controller's touch methods.

凝望流年 2024-11-11 12:53:42

我刚刚偶然发现了可能可以解决您问题的方法。创建表视图时使用此代码:

tableView.canCancelContentTouches = NO;

如果不将其设置为 NO,一旦表视图中出现轻微的垂直移动(如果您放置 NSLog 语句),触摸事件就会被取消在您的代码中,您会看到一旦表格开始垂直滚动,就会调用 touchesCancelled )。

I just stumbled onto what may be a solution for your problem. Use this code when you create your table view:

tableView.canCancelContentTouches = NO;

Without setting this to NO, the touch events are cancelled as soon as there is even a slight bit of vertical movement in your table view (if you put NSLog statements in your code, you'll see that touchesCancelled is called as soon as the table starts scrolling vertically).

○闲身 2024-11-11 12:53:42

我很长时间以来一直面临这个问题,但没有找到任何可行的解决方案。最后我选择了另一种选择。我知道从技术上讲这不是解决方案,但这肯定可以帮助寻找相同解决方案的人。

就我而言,我想选择一行,在触摸表或视图上的任何位置后,我想隐藏这些选项或执行除之前选择的行之外的任何任务,我执行了以下操作:

  1. 设置触摸事件为了景观。当您触摸除表格视图之外的视图上的任何位置时,这将执行任务。

  2. TableView 的 didSelectRowAtIndexPath 执行以下操作

     - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
        if(indexPath.row != previousSelectedRow && previousSelectedRow != -1) {
            // 隐藏选项或任何你想做的事情
            先前选定的行 = -1;
        }
        别的 {
            // 显示你的选项或其他东西
            previousSelectedRow = indexPath.row;
        }
     }
    

我知道这是较旧的帖子,不是一个好的技术解决方案,但这对我有用。我发布这个答案是因为这肯定会对某人有所帮助。

注意:这里写的代码可能有拼写错误,因为直接在这里输入。 :)

I was facing the problem since a long time and didn't got any working solution. Finally I choose to go with a alternative. I know technically this is not the solution but this may help someone looking for the same for sure.

In my case I want to select a row that will show some option after that I touch anywhere on table or View I want to hide those options or do any task except the row selected previously for that I did following:

  1. Set touch events for the view. This will do the task when you touch anywhere on the view except the table view.

  2. TableView's didSelectRowAtIndexPath do following

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

I know that this is older post and not a good technical solution but this worked for me. I am posting this answer because this may help someone for sure.

Note: The code written here may have spell mistakes because directly typed here. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文