在点击UITableView时会返回resignFirstResponder? [英] resignFirstResponder when tapping UITableView?
问题描述
我有一个带有两个自定义单元格的 UITableView
,每个单元格中都有一个带有 UITextField
的子视图.我尝试在 UITableView
顶部添加 UIButton
并将其设置为 resignFirstResponder
,但这仅意味着您将无法点击其他任何位置-甚至没有在 UITextFields
上输入文本.
I have a UITableView
with two custom cells and each of them has a subview with a UITextField
inside it. I have tried adding a UIButton
on top of the UITableView
and have it resignFirstResponder
but that just means you won't be able to tap anywhere else - not even on the UITextFields
to enter text.
如何做到这一点,如果我在这两个 UITextFields
之外点击,我可以调用resignFirstResponder,以便用户可以返回到其他单元格?
How do I make it so if I tap outside those two UITextFields
, I can call resignFirstResponder, so the user can get back to the other cells?
谢谢
推荐答案
我问了一个类似的我自己找到了答案,并发布了该答案.基本上,您可以在自定义UITableViewCell中重写hitTest:withEvent:,它使您能够检测到单元格或表视图中任何其他单元格何时发生触摸.我提供了一个示例hitTest:方法,该方法复制了原始hitTest:的功能,您可以在其中添加所需的内容.您可以像我一样在hitTest函数中添加resignFirstResponder或进一步自定义它.
I asked a similar question and found an answer myself which I posted. Basically you can override hitTest:withEvent: in your custom UITableViewCell which lets you detect when a touch occurred in your cell or any other cell that is in the table view. I provided an example hitTest: method that duplicates the functionality of the original hitTest: and you can just add what you need to it. You can add your resignFirstResponder inside the hitTest function like I did or customize it further.
这篇关于在点击UITableView时会返回resignFirstResponder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!