didSelectRowAtIndexPath:没有被调用
我有一个 UITableView
作为我的 UIScrollVIew
的子视图,它是由我的 MainViewController
控制的主视图。
在 MainViewController.h 中
@interface MainViewController : UIViewController <UIGestureRecognizerDelegate, UITableViewDelegate, UITableViewDataSource>
// other stuff here...
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
在 MainViewController.m
@synthesize myTableView;
// other stuff here...
- (void)viewDidLoad {
myTableView.delegate = self;
myTableView.datasource = self;
}
// other stuff here...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"listAttributesSegue" sender:self];
}
中,我知道 didSelectRowAtIndexPath
没有被调用,因为我在方法本身和其中的代码行上设置了断点,并且都没有被调用。我还知道数据源工作正常,因为我有其他函数可以在运行时修改单元格,并且它们工作得很好。我使用最新的 Xcode,并将 iOS 5.0 设置为开发目标。我一直在寻找并寻找答案。有人有什么想法吗?
编辑: 我已经找到答案了。我为 myTableView 的 superView 设置了一个 UITapGestureRecognizer
。这覆盖了选择调用。感谢任何建议可能就是这样的人。在我将其标记为正确之前,您的答案已被删除。
编辑2: 很多人都在评论这个问题,所以我想分享一下。如果您遇到此问题,只需将 myGestureRecognizer.cancelsTouchInView
设置为 false
即可,一切都会正常工作。
I have a UITableView
as a subview of my UIScrollVIew
, which is the main view controlled by my MainViewController
.
In MainViewController.h
@interface MainViewController : UIViewController <UIGestureRecognizerDelegate, UITableViewDelegate, UITableViewDataSource>
// other stuff here...
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
In MainViewController.m
@synthesize myTableView;
// other stuff here...
- (void)viewDidLoad {
myTableView.delegate = self;
myTableView.datasource = self;
}
// other stuff here...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"listAttributesSegue" sender:self];
}
I know that didSelectRowAtIndexPath
is not being called because I have set breakpoints on both the method itself and the line of code inside it, and neither is being called. I also know that the datasource is working correctly because I have other functions which modify the cells at runtime and they are working perfectly fine. I am using the latest Xcode with iOS 5.0 set as the development target. I have searched and searched for an answer. Anyone have any ideas?
Edit:
I have found the answer. I had a UITapGestureRecognizer
set for myTableView's superView. This overrode the selection call. Credit to whoever suggested that that might be it. Your answer was deleted before I could mark it correct.
Edit 2:
A lot of people have been commenting about this, so I though I would share it. If you are experiencing this problem, simply set myGestureRecognizer.cancelsTouchInView
to false
and everything should work fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我已经找到答案了。我为 myTableView 的 superView 设置了 UITapGestureRecognizer。这覆盖了选择调用。感谢任何建议可能就是这样的人。在我将其标记为正确之前,您的答案已被删除。
将手势识别器上的
cancelsTouchesInView
属性设置为NO
以允许表视图拦截该事件。I have found the answer. I had a UITapGestureRecognizer set for myTableView's superView. This overrode the selection call. Credit to whoever suggested that that might be it. Your answer was deleted before I could mark it correct.
Set the
cancelsTouchesInView
property toNO
on the gesture recogniser to allow the table view to intercept the event.如果您在代码中使用了 UITapGestureRecognizer :- # Swift 3
使用下面的代码行:
如何调用:-
在 ViewDidLoad() 中
if you are used UITapGestureRecognizer in your code :- # Swift 3
use below lines of code:
How to called:-
In ViewDidLoad()
您的问题是区分大小写。您的代码:
应该是
Your problem is case-sensitivity. Your code:
should be
您是否为同名的表视图定义了实例变量。
如果不是,那么这可能是问题所在-
或者-
Have you defined instance variable for tableview with same name.
If not then might be this can be the issue-
Or-
也许这毕竟是一个错字。检查您的函数是否不是
didDeselectRowAtIndexPath:
(de select 而不是 select)。Maybe it is a typo after all. Check that your function is not
didDeselectRowAtIndexPath:
(de select instead of select).我的解决方案是:
将
cancelsTouchesInView
设置为No
任何tapGestureuserInteractionEnable
设置为NO
,只需删除userInteractionEnable = No
即可解决问题。My solution is :
set
cancelsTouchesInView
ToNo
of any tapGestureI found in my custom cell ,
userInteractionEnable
is set toNO
, simply deleteuserInteractionEnable = No
and issue solved.取消除所需视图之外的其他视图触摸。
Cancel the other views touches except required one.
抱歉,没有足够的积分来添加评论 - 加勒特的回答很棒,但我想补充一点:
您仍然可以使用手势识别器,但您需要将“取消视图中的触摸”设置为“否” - 然后将传递手势到视图中,您的 UITableView 将正常工作。
在尝试了很多很多方法之后,这似乎是正确的做事方法:具有“取消视图中的触摸”的点击手势识别器就像在所有事件之上有一个不可见的层,它捕获所有事件并将它们路由到视图控制器(代理)。然后,视图控制器查看手势以查看它是否具有操作绑定(按钮等),并将路由这些操作,其余的将仅转到手势处理程序。当使用 UITableView 时,它期望接收点击,但当您“取消视图中的触摸”时,视图控制器会捕获它。
Sorry, haven't got enough points to add comments - Garret's answer is great but I would add:
You can still have your gesture recognizer but you will need to set 'Cancels touches in view' to NO - then the gestures will be handed on to the view and your UITableView will work fine.
After trying many, many approaches this seems to be the correct way of doing things: a tap gesture recognizer with 'cancel touches in view' is like having an invisible layer on top of everything that grabs all the events and routes them to the view controller (the proxy). The view controller then looks at the gesture to see if it has an action binding (buttons etc.) and will route those and any remaining will just go to the gesture handler. When using a UITableView it is expecting to receive the tap but the view controller snaffles it when you have 'Cancels touches in view'.
我遇到这个问题有一段时间了,我在这里没有看到任何对它的引用,所以作为参考,另一个原因可能是:
但是
根据文档
I was having this issue for a while, and I did not see any reference to it here, so for reference, another reason for this could be that:
but
As per documentation for
用户可以通过调用“tableView.selectRowAtIndexPath(..)”或“cell.setSelected(true, ...)”来选择单元格(点击行)。
如果通过调用“选择单元格” cell.setSelected(true)”,用户
无法再取消选择单元格。
如果通过调用选择了单元格
“tableView.selectRowAtIndexPath()”,用户可以取消选择单元格
预期。
A cell can be selected by the user (tapping on the row), by calling "tableView.selectRowAtIndexPath(..)" or "cell.setSelected(true, ...).
If the cell is selected by calling "cell.setSelected(true)", the user
cannot deselect the cell anymore.
If the cell is selected by calling
"tableView.selectRowAtIndexPath()", the user can deselect the cell as
expected.
我的情况很奇怪。我的 tableView 有 2 个部分。第一部分的单元格在
tableView:didSelectRowAt:
上工作正常,但第二部分的单元格不会触发didSelectRowAt:
。上述问题发生在
iPhone 4s、iOS 9.3
上。但在iPhone 5s、iOS 10.3
中,没有任何问题,这些电池工作正常。看起来像是关于UITableView
的iOS 9
错误。经过多次测试,我发现一行代码产生了这个错误。
因为第二部分没有标题视图。我删除了这一行,一切正常。
My case is strange. My tableView has 2 sections. 1st section's cells work fine about
tableView:didSelectRowAt:
, but 2nd section's cells doesn't triggerdidSelectRowAt:
.The above problem happens at
iPhone 4s, iOS 9.3
. But iniPhone 5s, iOS 10.3
, there are no problems, those cells works fine. It seems likeiOS 9
bugs aboutUITableView
.After many tests, I found out one line codes produces this bug.
Because the 2nd sections has no header view. I remove this line, and all works fine.
我在自定义单元格按下时遇到了 didSelectRowAtIndexPath: 间歇性失败。
我发现,如果我停止经常调用 [tableView reloadData] (10 Hz),并将其更改为每 2 秒更新一次,则几乎每次按下都会成功调用 didSelectRowAtIndexPath:
似乎重新加载视图会阻止按下。
I had intermittent failure of didSelectRowAtIndexPath: being called on my custom cell press.
I discovered that if I stopped calling [tableView reloadData] very often (10 Hz), and changed it to update every 2 seconds, almost every press would successfully call didSelectRowAtIndexPath:
It seems like reloading the view blocks presses.
我的问题是该单元格是自定义单元格,并且该操作对其不起作用。此外,超类中定义了一个
UITapGestureRecognizer
。首先,
其次,设置
tapGesture.cancelsTouchesInView = false
,而不是设置
isUserInteractionEnabled = true;
在表视图中,我在单元格上设置了操作。在 ViewDidLoad()
然后在
如果您要创建自定义单元格,您可以尝试此解决方案。
My problem is the cell is a customized cell, and the action does not work on it. In addition, there is a
UITapGestureRecognizer
defined in the superclass.Firstly,
Set
tapGesture.cancelsTouchesInView = false
Secondly, Instead of setting
isUserInteractionEnabled = true;
in the table view, I set the action on the cell.In the ViewDidLoad()
Then in the
You can try this solution if you are creating a customized cell.
这对我来说很有效,你能尝试一下吗?
让点击:UITapGestureRecognizer = UITapGestureRecognizer(目标:self,操作:#selector(self.dismissKeyboard))
点击.cancelsTouchesInView = false
view.addGestureRecognizer(点击)
It's work for me, can you try!
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)