ios didSelectRowAtIndexPath:没有被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8952688/
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
didSelectRowAtIndexPath: not being called
提问by Garrett
I have a UITableView
as a subview of my UIScrollVIew
, which is the main view controlled by my MainViewController
.
我有一个UITableView
作为 my 的子视图UIScrollVIew
,它是由我的MainViewController
.
In MainViewController.h
在 MainViewController.h
@interface MainViewController : UIViewController <UIGestureRecognizerDelegate, UITableViewDelegate, UITableViewDataSource>
// other stuff here...
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
In MainViewController.m
在 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?
我知道didSelectRowAtIndexPath
没有被调用,因为我在方法本身和其中的代码行上都设置了断点,并且都没有被调用。我也知道数据源工作正常,因为我有其他函数可以在运行时修改单元格,并且它们工作得很好。我正在使用最新的 Xcode,并将 iOS 5.0 设置为开发目标。我已经搜索并寻找答案。谁有想法?
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.
编辑:我找到了答案。我UITapGestureRecognizer
为 myTableView 的 superView 设置了一套。这覆盖了选择调用。归功于任何建议这可能是它的人。在我将其标记为正确之前,您的答案已被删除。
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.
编辑2:很多人一直在评论这个,所以我想我会分享它。如果您遇到此问题,只需设置myGestureRecognizer.cancelsTouchInView
为false
,一切都会正常工作。
回答by Garrett
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.
我找到了答案。我为 myTableView 的 superView 设置了一个 UITapGestureRecognizer。这覆盖了选择调用。归功于任何建议这可能是它的人。在我将其标记为正确之前,您的答案已被删除。
Set the cancelsTouchesInView
property to NO
on the gesture recogniser to allow the table view to intercept the event.
在手势识别器上将cancelsTouchesInView
属性设置NO
为允许表视图拦截事件。
回答by Kiran jadhav
Updated for Swift 3:
为 Swift 3 更新:
if you are used UITapGestureRecognizer in your code :- # Swift 3use below lines of code:
如果您在代码中使用了 UITapGestureRecognizer :- # Swift 3使用以下代码行:
extension YourViewController{
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(YourViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
tap.cancelsTouchesInView = false
}
func dismissKeyboard() {
view.endEditing(true)
}
}
How to called:-In ViewDidLoad()
如何调用:-在 ViewDidLoad()
self.hideKeyboardWhenTappedAround()
回答by A. Wilcox
Your problem is case-sensitivity. Your code:
您的问题是区分大小写。您的代码:
- (void)tableVIew:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
should be
应该
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
回答by Mundi
Maybe it is a typo after all. Check that your function is not didDeselectRowAtIndexPath:
(deselect instead of select).
也许这毕竟是一个错字。检查您的功能是否不是didDeselectRowAtIndexPath:
(deselect 而不是 select)。
回答by rishi
Have you defined instance variable for tableview with same name. If not then might be this can be the issue-
您是否为具有相同名称的 tableview 定义了实例变量。如果不是,那么可能是这可能是问题-
_myTableView.delegate = self;
_myTableView.datasource = self;
Or-
或者-
self.myTableView.delegate = self;
self.myTableView.datasource = self;
回答by PeiweiChen
My solution is :
我的解决方案是:
set
cancelsTouchesInView
ToNo
of any tapGestureI found in my custom cell ,
userInteractionEnable
is set toNO
, simply deleteuserInteractionEnable = No
and issue solved.
设置
cancelsTouchesInView
为No
任何 tapGesture我在我的自定义单元格中发现,
userInteractionEnable
设置为NO
,只需删除userInteractionEnable = No
并解决问题。
回答by MuraliMohan
Cancel the other views touches except required one.
取消除必需的视图之外的其他视图。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch {
if (touch.view == your view) {
return YES;
}
return NO;
}
回答by AechoLiu
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 trigger didSelectRowAt:
.
我的情况很奇怪。我的 tableView 有 2 个部分。第 1 部分的单元格工作正常tableView:didSelectRowAt:
,但第 2 部分的单元格不会触发didSelectRowAt:
。
The above problem happens at iPhone 4s, iOS 9.3
. But in iPhone 5s, iOS 10.3
, there are no problems, those cells works fine. It seems like iOS 9
bugs about UITableView
.
上述问题发生在iPhone 4s, iOS 9.3
. 但是在iPhone 5s, iOS 10.3
,没有问题,这些单元格工作正常。似乎是iOS 9
关于UITableView
.
After many tests, I found out one line codes produces this bug.
经过多次测试,我发现一行代码会产生此错误。
tableView.estimatedSectionHeaderHeight = 60.0
Because the 2nd sections has no header view. I remove this line, and all works fine.
因为第二部分没有标题视图。我删除了这一行,一切正常。
回答by Oliver Dungey
Sorry, haven't got enough points to add comments - Garret's answer is great but I would add:
抱歉,没有足够的分数来添加评论 - Garret 的回答很棒,但我想补充一点:
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.
您仍然可以使用手势识别器,但您需要将“取消视图中的触摸”设置为“否” - 然后手势将传递给视图,您的 UITableView 将正常工作。
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'.
在尝试了很多很多方法之后,这似乎是正确的做事方式:带有“取消视图中的触摸”的点击手势识别器就像在所有事件之上有一个不可见的层,它可以抓取所有事件并将它们路由到视图控制器(代理)。视图控制器然后查看手势以查看它是否具有动作绑定(按钮等),并将路由这些,任何剩余的都将转到手势处理程序。使用 UITableView 时,它希望收到点击,但是当您“取消视图中的触摸”时,视图控制器会对其进行处理。
回答by Bamaco
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:
我遇到这个问题有一段时间了,我在这里没有看到任何参考,所以作为参考,另一个原因可能是:
tableView.editing = YES;
but
但
tableView.allowsSelectionDuringEditing = NO;
As per documentation for
根据文档
- tableView:didSelectRowAtIndexPath:
This method isn't called when the editing property of the table is set to YES (that is, the table view is in editing mode). See "Managing Selections" in Table View Programming Guide for iOS for further information (and code examples) related to this method.
当表的编辑属性设置为 YES(即表视图处于编辑模式)时,不会调用此方法。有关此方法的更多信息(和代码示例),请参阅 iOS 表视图编程指南中的“管理选择”。