-didSelectRowAtIndexPath:未被调用
我正在编写一个 iOS 应用程序,在选项卡视图中包含表视图。 在我的 UITableViewController
中,我实现了 -tableView:didSelectRowAtIndexPath:
,但是当我在运行时选择一行时,该方法不会被调用。 不过,表视图正在填充,所以我知道控制器中的其他 tableView 方法正在被调用。
有谁知道我可能搞砸了什么才能实现这一目标?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
以防万一有人犯了和我一样的愚蠢错误:
检查一下您期望的
didSelect
的方法名称是否可能会以某种方式意外地获得didDeselect
。 我花了大约两个小时才发现...Just in case someone made the same stupid mistake as I did:
Check out if the method name of what you expect of being
didSelect
may accidentally be gottendidDeselect
in some way. It took about two hours for me to find out ...可能导致问题的另一件事是未选择选择类型:
应该是
单选 对于正常选择,不应该是
No Selection
。要以编程方式执行此操作,请执行以下操作:
Another thing that might lead to the issue is not selected selection kind:
Should be
Single Selection
for normal selection, should not beNo Selection
.To do this programmatically, do:
另一种可能性是 UITapGestureRecognizer 可能正在吞噬事件,如下所示: https://stackoverflow.com/a/9248827/ 214070
我并不怀疑这个原因,因为表格单元格仍然会突出显示蓝色,就好像水龙头正在通过一样。
Another possibility is that a UITapGestureRecognizer could be eating the events, as was the case here: https://stackoverflow.com/a/9248827/214070
I didn't suspect this cause, because the table cells would still highlight blue as if the taps were getting through.
所有好的答案,但还有一个需要注意...
(特别是在以编程方式创建 UITableView 时)
确保 tableView 可以通过设置
来响应选择[tableView setAllowsSelection:YES];
或删除任何将其设置为NO
的行。All good answers, but there's one more to look out for...
(Particularly when creating a UITableView programmatically)
Make sure the tableView can respond to selection by setting
[tableView setAllowsSelection:YES];
or removing any line that sets it toNO
.听起来该类可能不是该表视图的 UITableViewDelegate ,尽管 UITableViewController 应该自动设置。
您有机会将代表重置为其他班级吗?
It sounds like perhaps the class is not the
UITableViewDelegate
for that table view, thoughUITableViewController
is supposed to set that automatically.Any chance you reset the delegate to some other class?
如果
UITapGestureRecognizer
出现问题,您可以解决此问题:在 使用
Objective-C
编写的代码:使用
Swift
编写的代码:If the problem arise with
UITapGestureRecognizer
you can fix this:in code with
Objective-C
:in code with
Swift
:在这种情况下我遇到过两件事。
您可能忘记实现 UITableViewDelegate 协议,或者您的类和表视图之间没有委托出口。
您可能忘记实现 UITableViewDelegate 协议,或者您的类和表视图之间没有委托
您的行中可能有一个 UIView,它是第一响应者并带走您的点击。 说一个 UIButton 或类似的东西。
I have encountered two things in this situations.
You may have forgot to implement UITableViewDelegate protocol, or there's no delegation outlet between your class and your table view.
You might have a UIView inside your row that is a first responder and takes your clicks away. Say a UIButton or something similar.
我曾经也有过一样的问题。 而且很难找到。 但在我的代码中的某个地方
是这样的:
它必须
返回indexPath
,否则-tableView:didSelectRowAtIndexPath:
不会被调用。I have had the same problem. And it was hard to find. But somewhere in my code
was this:
It must be
return indexPath
, else-tableView:didSelectRowAtIndexPath:
is not being called.如果您在 UITableView 顶部添加了gestureRecognizer,则不会调用
didSelectRowAtIndexPath
。因此,您需要使用gestureRecognizer委托方法来避免特定视图中的触摸。
If you added a gestureRecognizer on top of the UITableView,
didSelectRowAtIndexPath
will not get called.So you need to use gestureRecognizer delegate method to avoid touch in particular view.
我遇到了一个问题,在几个月没有查看我的代码之后,我忘记了由于一些不必要的要求而实现了以下方法,
它应该为一行返回 YES 才能使其被选中。
I've ran into a problem where after months of not looking at my code I forgot that I implemented the following method due to some requirements which were not necessary
It should be returning YES for a row in order to make it selected.
您必须选择这些选项
但如果您想让
UITableView
在单击时不突出显示,那么您应该在UITableViewCell
属性中进行更改。选择“无”选项,如下所示
YOU MUST select these options
but if you want to make
UITableView
not highlighted on clicking then you should make changes inUITableViewCell
properties.Choose None option for Selection just like below
我在表格视图上放置了一个 UITapGestureRecognizer 来关闭键盘,从而阻止调用 didSelectRowAtIndexPath: 。 希望它能帮助某人。
I had put a
UITapGestureRecognizer
on my table view to dismiss the keyboard which preventeddidSelectRowAtIndexPath:
from being called. Hope it helps someone.如果你遇到和我一样的问题:
显然,如果您的 tableView 处于编辑模式,则不会调用此方法。 您必须将 allowedSelectionDuringEditing 设置为 true。
通过这个问题: 编辑时,`UITableView`不会调用 didSelectRowAtIndexPath ??
In case you have the same problem as me:
Apparently, this method won't be called if your tableView is in edit mode. You have to set allowsSelectionDuringEditing to true.
Via this question: When editing, `UITableView` does not call didSelectRowAtIndexPath ??
我遇到了同样的问题,
原因是使用了
UITapGestureRecognizer
。 我希望当我点击其他地方时键盘会关闭。 我意识到这会覆盖所有点击操作,这就是为什么didSelectRowAtIndexPath
函数没有被调用。当我评论与
UITapGestureRecognizer
相关的行时,它起作用了。 此外,您可以检查UITapGestureRecognizer选择器
的功能,是否点击的是UITableViewCell
。I had the same problem,
The reason was using of
UITapGestureRecognizer
. I wanted the keyboard to dismiss when I tapped anywhere else. I realized that this overrides all tap actions, that is why,didSelectRowAtIndexPath
function did not called.When I comment the rows related with
UITapGestureRecognizer
, it works. Moreover you can check in the function ofUITapGestureRecognizer selector
if the tapped isUITableViewCell
or not.就我而言,didSelctRowAtIndexPath未调用是因为我在tableView的Selection属性中选择了none,设置为单选解决了我的问题
In my case, didSelctRowAtIndexPath not calling is due to that I have selected none in the Selection property of tableView, set to single selection solved my problem
对于 Xcode 6.4、Swift 1.2 。 IB 中的选择“标签”已更改。 我不知道如何以及为什么。 将其设置为“单选”使我的表格视图单元格再次可选择。
For Xcode 6.4, Swift 1.2 . The selection "tag" had been changed in IB. I don't know how and why. Setting it to "Single Selection" made my table view cells selectable again.
即使另一个答案已被接受,我也会为观察到此问题的人添加一个可能的问题和解决方案:
如果您打开了自动引用计数(ARC),您可能会发现即使在将控制器分配为在视图中,由于 ARC 正在删除控制器,因此未收到视图发送给控制器的消息。 显然 UITableView 的委托指针不算作 ARC 的引用,因此如果这是对它的唯一引用,控制器将被释放。 您可以通过在控制器上实现 dealloc 方法并在那里设置断点或 NSLog 调用来验证是否发生这种情况。
解决方案是在其他地方使用强引用来跟踪控制器,直到您确定不再需要它为止。
Even though another answer has been accepted, I'll add one more possible problem and solution for people who observe this issue:
If you have automatic reference counting (ARC) turned on, you may find that even after assigning your controller as a delegate of the view, the view's messages to the controller are not being received because ARC is deleting the controller. Apparently the UITableView's delegate pointer does not count as a reference for the ARC, so if that is the only reference to it, the controller will be dealloc'd. You can verify whether or not this is happening by implementing the dealloc method on the controller and setting a breakpoint or NSLog call there.
The solution is to keep track of the controller with a strong reference somewhere else, until you are sure you won't need it anymore.
请记住在 viewDidLoad 方法中设置数据源和委托,如下所示:
Remember to set the datasource and delegate in the viewDidLoad method as follows:
我的问题不是以上任何一个。 又那么蹩脚。 但我想我会把它列在这里,以防它对某人有帮助。
我有一个 tableViewController ,它是我的“基本”控制器,然后我创建该控制器的子类。 我在“基”类的
tableView:didSelectRowAtIndexPath
例程中编写了所有代码。 完全忘记了默认情况下也在我的所有子类中创建了这个例程(尽管没有执行任何操作的代码)。 因此,当我运行我的应用程序时,它运行了代码的子类版本,什么也没做,这让我感到难过。 当然,一旦我从子类中删除了例程,它就会使用 mt“基”类例程,我就开始工作了。我知道。 别笑。 但也许这会拯救某人我失去的时间......
My problem was none of the above. And so lame. But I thought I would list it here in case it helps someone.
I have a
tableViewController
that is my "base" controller and then I create subclasses of this controller. I was writing all my code in thetableView:didSelectRowAtIndexPath
routine in the "base" class. Completely forgetting that by default this routine had also been created (albeit with no code that did anything) in all of my subclasses as well. So when I ran my app, it ran the subclass version of the code, did nothing, and made me sad. So of course, once I removed the routine from the subclasses, it used mt "base" class routine and I'm in business.I know. Don't laugh. But maybe this will save someone the hour I lost...
对此我付出 2 美分。
我有一个自定义 UITableViewCell,并且有一个按钮覆盖整个单元格,因此当发生触摸时,会选择按钮而不是单元格。
要么删除按钮,要么在我的例子中,我将按钮上的用户交互启用设置为 false,这样单元格就被选中了。
Giving my 2 cents on this.
I had a Custom UITableViewCell and there was a button covering the whole cell, so when the touch happened, the button was selected and not the cell.
Either remove the button or in my case, I set User Interation Enable to false on the button, that way the cell was the one selected.
如果你读了这篇文章,那么仍然没有解决问题。
我有自定义单元格,其中“启用用户交互”复选框被禁用。 所以,我只是打开它。 祝你好运。
If you read this, so still doesn't solve the problem.
I have custom cell, where checkbox "User Interaction Enabled" was disable. So, I Just switch on it. Good luck.
我刚刚遇到了这个,就像过去发生在我身上的那样,它不起作用,因为我在尝试添加方法时没有注意自动完成,实际上我最终实现了
tableView:didDeselectRowAtIndexPath
:而不是tableView:didSelectRowAtIndexPath:
。I just had this and as has happened to me in the past it didn't work because I didn't pay attention to the autocomplete when trying to add the method and I actually end up implementing
tableView:didDeselectRowAtIndexPath
: instead oftableView:didSelectRowAtIndexPath:
.确保您实现了
tableView:didSelectRowAtIndexPath
而不是tableView:didDeSelectRowAtIndexPath
这让我多次陷入困境!
Make sure you implemented
tableView:didSelectRowAtIndexPath
and nottableView:didDeSelectRowAtIndexPath
This is gotten me on more than a few occasions !!
请检查 UITapGestureRecognizer。
在我的例子中,为放置 tableview 的视图添加了 tapgesture,这会像 didselect 一样消耗 UITableview 的用户交互。
禁用视图的点击手势后,触发了 didselect delegate。
Please check for UITapGestureRecognizer.
In my case tapgesture was added for the view where tableview got placed, which is eating the user interaction of UITableview like didselect.
After disabling tapgesture for the view, didselect delegate was triggered.
如果您的表格视图处于编辑模式(例如
[tableView setEditing:YESanimated:NO];
),您需要设置tableView.allowsSelectionDuringEditing = YES;
If your table view is in editing mode (eg.
[tableView setEditing:YES animated:NO];
), you need to settableView.allowsSelectionDuringEditing = YES;
就我而言,解决方案是在以下函数中将“否”更改为“是”。
iOS 9+
In my case the solution was to change NO to YES in the below function.
iOS 9+
检查您的
viewController
是否有以下方法:如果返回“NO”则不会调用 didSelectRow
Check if your
viewController
has following method:if you return "NO" didSelectRow won't be called
我知道问题已经解决了,但有类似的问题,我认为问题出在我的自定义 UITableViewCell 上,但解决方案完全不同 - 我重新启动 XCode :) 然后工作正常! 几乎就像 Windows 一样:)
I know is old and the problem was resolved, but a had similar problem, I thought that the problem was with my custom UITableViewCell, but the solution was completely different - I restart XCode :) and then works ok ! almost like Windows :)
您可能犯的另一个错误(就像我所做的那样):如果您在单元格中设置了转场,则不会调用
didSelectRowAtIndexPath
。 您应该在视图控制器上设置 Segues。Another mistake you could've done (as I did): if you set a segue at the cell,
didSelectRowAtIndexPath
is not called. You should set your segues at the view controller instead.这些答案都不适合我。 大约一个小时后,我发现了一些非常阴险的事情:
我在另一个表视图的单元格内有一个表视图。 我决定创建一个包含内部表视图等的封闭视图。 我将此视图称为 contentView 并将其连接到 xib 中。
事实证明 UITableViewCell 已经有一个 contentView 并用它做了奇怪的事情。 当我将属性重命名为 mainContentView 并将视图重新连接到此重命名的属性时,问题自行解决。
None of these answers worked for me. After about an hour, I figured out something very insidious:
I have a table view inside a cell of another table view. I decided to make an enclosing view that contains the inner table view, among other things. I called this view contentView and hooked it up in the xib.
Turns out that UITableViewCell already has a contentView and does weird things with it. The issue resolved itself when I renamed the property to mainContentView and reconnected the view to this renamed property.