ios -didSelectRowAtIndexPath: 没有被调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/255927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:21:44  来源:igfitidea点击:

-didSelectRowAtIndexPath: not being called

iosuitableviewcocoa-touchdidselectrowatindexpath

提问by Matt Pfefferle

I'm writing an iOS app with a table view inside a tab view. In my UITableViewController, I implemented -tableView:didSelectRowAtIndexPath:, but when I select a row at runtime, the method isn't being called. The table view is being populated though, so I know that other tableView methods in my controller are being called.

我正在编写一个在选项卡视图中带有表视图的 iOS 应用程序。在我的 中UITableViewController,我实现了-tableView:didSelectRowAtIndexPath:,但是当我在运行时选择一行时,没有调用该方法。表视图正在被填充,所以我知道我的控制器中的其他 tableView 方法正在被调用。

Does anyone have any ideas what I may have screwed up to make this happen?

有没有人知道我可能搞砸了什么来实现这一目标?

采纳答案by Hunter

It sounds like perhaps the class is not the UITableViewDelegatefor that table view, though UITableViewControlleris supposed to set that automatically.

听起来可能该类不是该UITableViewDelegate表视图的类,尽管UITableViewController应该自动设置。

Any chance you reset the delegate to some other class?

您是否有机会将委托重置为其他课程?

回答by CGee

Just in case someone made the same stupid mistake as I did:

以防万一有人犯了和我一样的愚蠢错误:

Check out if the method name of what you expect of being didSelectmay accidentally be gotten didDeselectin some way. It took about two hours for me to find out ...

检查您期望的方法名称是否didSelect可能didDeselect以某种方式意外获得。我花了大约两个小时才发现......

回答by Dennis Krut

Another thing that might lead to the issue is not selected selection kind:

可能导致问题的另一件事是未选择的选择类型:

UITableView selection kind

UITableView 选择种类

Should be Single Selectionfor normal selection, should notbe No Selection.

应该是Single Selection正常选择,应该No Selection

To do this programmatically, do:

要以编程方式执行此操作,请执行以下操作:

tableView.allowsSelection = YES

回答by bugloaf

Another possibility is that a UITapGestureRecognizer could be eating the events, as was the case here: https://stackoverflow.com/a/9248827/214070

另一种可能性是 UITapGestureRecognizer 可能正在吞噬事件,就像这里的情况: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.

我没有怀疑这个原因,因为表格单元格仍然会突出显示蓝色,就好像水龙头正在通过一样。

回答by Old McStopher

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];or removing any line that sets it to NO.

确保 tableView 可以通过设置[tableView setAllowsSelection:YES];或删除将其设置为 的任何行来响应选择NO

回答by Bart?omiej Semańczyk

If the problem arise with UITapGestureRecognizeryou can fix this:

如果出现问题,UITapGestureRecognizer您可以解决此问题:

  • in Storyboard:
  • 在故事板中:

enter image description here

在此处输入图片说明

in code with Objective-C:

在代码中Objective-C

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; 
[self.view addGestureRecognizer:tap];

[tap setCancelsTouchesInView:NO];

in code with Swift:

在代码中Swift

let tap = UITapGestureRecognizer(target: self, action:Selector("dismissKeyboard"))
view.addGestureRecognizer(tap)

tap.cancelsTouchesInView = false

回答by gilm

I have encountered two things in this situations.

在这种情况下,我遇到了两件事。

  1. You may have forgot to implement UITableViewDelegate protocol, or there's no delegation outlet between your class and your table view.

  2. You might have a UIView inside your row that is a first responder and takes your clicks away. Say a UIButton or something similar.

  1. 你可能忘记实现 UITableViewDelegate 协议,或者你的类和你的表视图之间没有委托出口。

  2. 您的行内可能有一个 UIView,它是第一响应者并带走您的点击。说一个 UIButton 或类似的东西。

回答by HymanPearse

I have had the same problem. And it was hard to find. But somewhere in my code was this:

我曾经也有过一样的问题。而且很难找到。但是在我的代码中的某个地方是这样的:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

It must be return indexPath, else -tableView:didSelectRowAtIndexPath:is not being called.

它必须是return indexPath,否则-tableView:didSelectRowAtIndexPath:不会被调用。

回答by Vinu David Jose

If you added a gestureRecognizer on top of the UITableView, didSelectRowAtIndexPathwill not get called.

如果您在 UITableView 之上添加了一个gestureRecognizer,didSelectRowAtIndexPath则不会被调用。

So you need to use gestureRecognizer delegate method to avoid touch in particular view.

所以你需要使用gestureRecognizer委托方法来避免特定视图中的触摸。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:YourTable]) {
        return NO;
    }
    return YES;
}

回答by cpt.neverm1nd

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

我遇到了一个问题,几个月没有查看我的代码后,我忘记了由于一些不必要的要求而实现了以下方法

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath  *)indexPath{
    return NO;
}

It should be returning YES for a row in order to make it selected.

它应该为一行返回 YES 以使其被选中。