UITableView 上的触摸事件?
我有 UIViewController
和 UITableView
作为视图中的子项, 我想要做的是,当我触摸任何行时,我会在底部显示一个视图。如果用户触摸行或底部视图之外的任何其他位置,我想隐藏该视图。
问题是当我单击 UITableView
时,它不会触发 touchesEnded
事件。
现在我如何检测 UITableView
上的触摸并将其与行选择事件区分开来。
谢谢。
I have UIViewController
and 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试以下方法:
使用scrollViewDidEndDragging 替代touchesEnded。希望有帮助。
Try this methods:
Use scrollViewDidEndDragging like alternative of touchesEnded. Hope it helps.
要接收
UITableView
上的触摸事件,请使用:To receive touch events on the
UITableView
use:在您的控制器类中声明一个删除底部视图的方法。像这样的事情:
在 Interface Builder 中,选择您的视图,然后在自定义类部分的身份检查器中,将类从
UIView
更改为UIControl
。之后,转到连接检查器并将 TouchUpInside 事件连接到上面声明的方法。希望这有帮助。In your controller class declare a method which removes the bottom view. Something like this:
In Interface Builder, select your view and in the identity inspector in the custom class section, change the class from
UIView
toUIControl
. After that go to the connections inspector and connect the TouchUpInside event to the method declared above. Hope this helps.无需子类化任何内容,您可以将
UITapGestureRecognizer
添加到UITableView
并根据您的标准吸收或不吸收手势。在您的 viewDidLoad 中:
然后,针对条件实现这样的操作:
请注意,如果您只想简单地拾取表格上任意位置的点击,而不是单元格行中的任何按钮的点击,则只需使用上面的第一个代码片段。一个典型的例子是当你有一个 UITableView 并且还有一个 UISearchBar 时。当用户单击、滚动表视图等时,您希望消除搜索栏。代码示例...
希望它对某人有帮助。
No need to subclass anything, you can add a
UITapGestureRecognizer
to theUITableView
and absorb the gesture or not depending on your criteria.In your viewDidLoad:
Then, implement your action like this for the criteria:
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...
Hope it helps someone.
您应该将触摸事件转发到视图的控制器。
子类化您的 tableview 控件,然后重写该方法:
然后,您可以在控制器的触摸方法中执行您想要的操作。
You should forward the touch event to the view's controller.
Subclass your tableview control and then override the method:
then , you can do what you want in the controller's touch methods.
我刚刚偶然发现了可能可以解决您问题的方法。创建表视图时使用此代码:
如果不将其设置为
NO
,一旦表视图中出现轻微的垂直移动(如果您放置 NSLog 语句),触摸事件就会被取消在您的代码中,您会看到一旦表格开始垂直滚动,就会调用touchesCancelled
)。I just stumbled onto what may be a solution for your problem. Use this code when you create your table view:
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 thattouchesCancelled
is called as soon as the table starts scrolling vertically).我很长时间以来一直面临这个问题,但没有找到任何可行的解决方案。最后我选择了另一种选择。我知道从技术上讲这不是解决方案,但这肯定可以帮助寻找相同解决方案的人。
就我而言,我想选择一行,在触摸表或视图上的任何位置后,我想隐藏这些选项或执行除之前选择的行之外的任何任务,我执行了以下操作:
设置触摸事件为了景观。当您触摸除表格视图之外的视图上的任何位置时,这将执行任务。
TableView 的 didSelectRowAtIndexPath 执行以下操作
我知道这是较旧的帖子,不是一个好的技术解决方案,但这对我有用。我发布这个答案是因为这肯定会对某人有所帮助。
注意:这里写的代码可能有拼写错误,因为直接在这里输入。 :)
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:
Set touch events for the view. This will do the task when you touch anywhere on the view except the table view.
TableView's didSelectRowAtIndexPath do following
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. :)