-didSelectRowAtIndexPath:不被调用
本文翻译自:-didSelectRowAtIndexPath: not being called
I'm writing an iOS app with a table view inside a tab view. 我正在写一个带有选项卡视图内的表格视图的iOS应用。 In my UITableViewController
, I implemented -tableView:didSelectRowAtIndexPath:
, but when I select a row at runtime, the method isn't being called. 在我的UITableViewController
,我实现了-tableView:didSelectRowAtIndexPath:
但是当我在运行时选择一行时,不会调用该方法。 The table view is being populated though, so I know that other tableView methods in my controller are being called. 虽然正在填充表视图,所以我知道正在调用控制器中的其他tableView方法。
Does anyone have any ideas what I may have screwed up to make this happen? 有谁知道我可能为实现这一目标而搞砸了什么?
#1楼
参考:https://stackoom.com/question/14zR/didSelectRowAtIndexPath-不被调用
#2楼
All good answers, but there's one more to look out for... 所有好的答案,但还有更多需要注意的地方...
(Particularly when creating a UITableView programmatically) (特别是在以编程方式创建UITableView时)
Make sure the tableView can respond to selection by setting [tableView setAllowsSelection:YES];
通过设置[tableView setAllowsSelection:YES];
来确保tableView可以响应选择[tableView setAllowsSelection:YES];
or removing any line that sets it to NO
. 或删除任何将其设置为NO
。
#3楼
Giving my 2 cents on this. 为此给我2美分。
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. 我有一个Custom UITableViewCell,并且有一个覆盖整个单元格的按钮,因此当发生触摸时,该按钮而不是该单元格被选中。
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. 要么删除按钮,要么就我而言,我将按钮上的User Interation Enable设置为false,这样该单元格即被选中。
#4楼
If you read this, so still doesn't solve the problem. 如果您阅读此内容,那么仍然无法解决问题。
I have custom cell, where checkbox " User Interaction Enabled " was disable. 我有自定义单元格,其中复选框“ User Interaction Enabled ”已禁用。 So, I Just switch on it. 所以,我只是打开它。 Good luck. 祝好运。
#5楼
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 gotten didDeselect
in some way. 看看,如果你希望成为什么样的方法名didSelect
可能会意外地得到didDeselect
以某种方式。 It took about two hours for me to find out ... 我花了大约两个小时才找到...
#6楼
Another thing that might lead to the issue is not selected selection kind: 可能导致问题的另一件事不是选定的选择类型:
Should be Single Selection
for normal selection, should not be No Selection
. 应该是Single Selection
正常的选择, 不应该是No Selection
。
To do this programmatically, do: 要以编程方式执行此操作,请执行以下操作:
tableView.allowsSelection = YES