didSelectRowAtIndexPath:没有被调用

发布于 2024-12-28 05:52:41 字数 1279 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

李不 2025-01-04 05:52:41

我已经找到答案了。我为 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 to NO on the gesture recogniser to allow the table view to intercept the event.

寄居人 2025-01-04 05:52:41

针对 Swift 3 进行了更新:

如果您在代码中使用了 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)
    }
}

如何调用:-
在 ViewDidLoad() 中

self.hideKeyboardWhenTappedAround()

Updated for Swift 3:

if you are used UITapGestureRecognizer in your code :- # Swift 3
use below lines of code:

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()

self.hideKeyboardWhenTappedAround()
书间行客 2025-01-04 05:52:41

您的问题是区分大小写。您的代码:

- (void)tableVIew:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {

应该是

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {

Your problem is case-sensitivity. Your code:

- (void)tableVIew:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {

should be

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
西瓜 2025-01-04 05:52:41

您是否为同名的表视图定义了实例变量。
如果不是,那么这可能是问题所在-

_myTableView.delegate = self;
_myTableView.datasource = self;

或者-

self.myTableView.delegate = self;
self.myTableView.datasource = self;

Have you defined instance variable for tableview with same name.
If not then might be this can be the issue-

_myTableView.delegate = self;
_myTableView.datasource = self;

Or-

self.myTableView.delegate = self;
self.myTableView.datasource = self;
烟火散人牵绊 2025-01-04 05:52:41

也许这毕竟是一个错字。检查您的函数是否不是 didDeselectRowAtIndexPath:de select 而不是 select)。

Maybe it is a typo after all. Check that your function is not didDeselectRowAtIndexPath: (de select instead of select).

挽清梦 2025-01-04 05:52:41

我的解决方案是:

  1. cancelsTouchesInView设置为No任何tapGesture

  2. userInteractionEnable 设置为
    NO,只需删除 userInteractionEnable = No 即可解决问题。

My solution is :

  1. set cancelsTouchesInView To No of any tapGesture

  2. I found in my custom cell , userInteractionEnable is set to
    NO, simply delete userInteractionEnable = No and issue solved.

怪异←思 2025-01-04 05:52:41

取消除所需视图之外的其他视图触摸。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch {
    if (touch.view == your view) {
        return YES;
    }
    return NO;
}

Cancel the other views touches except required one.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch {
    if (touch.view == your view) {
        return YES;
    }
    return NO;
}
触ぅ动初心 2025-01-04 05:52:41

抱歉,没有足够的积分来添加评论 - 加勒特的回答很棒,但我想补充一点:

您仍然可以使用手势识别器,但您需要将“取消视图中的触摸”设置为“否” - 然后将传递手势到视图中,您的 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'.

祁梦 2025-01-04 05:52:41

我遇到这个问题有一段时间了,我在这里没有看到任何对它的引用,所以作为参考,另一个原因可能是:

tableView.editing = YES;

但是

tableView.allowsSelectionDuringEditing = NO;

根据文档

- tableView:didSelectRowAtIndexPath:

当表的编辑属性设置为YES(即表视图处于编辑模式)时,不会调用此方法。有关此方法的更多信息(和代码示例),请参阅 iOS 表格视图编程指南中的“管理选择”。

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.

若水微香 2025-01-04 05:52:41

用户可以通过调用“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.

清醇 2025-01-04 05:52:41

我的情况很奇怪。我的 tableView 有 2 个部分。第一部分的单元格在 tableView:didSelectRowAt: 上工作正常,但第二部分的单元格不会触发 didSelectRowAt:

上述问题发生在 iPhone 4s、iOS 9.3 上。但在 iPhone 5s、iOS 10.3 中,没有任何问题,这些电池工作正常。看起来像是关于 UITableViewiOS 9 错误。

经过多次测试,我发现一行代码产生了这个错误。

tableView.estimatedSectionHeaderHeight = 60.0

因为第二部分没有标题视图。我删除了这一行,一切正常。

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:.

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.

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.

请你别敷衍 2025-01-04 05:52:41

我在自定义单元格按下时遇到了 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.

生生漫 2025-01-04 05:52:41

我的问题是该单元格是自定义单元格,并且该操作对其不起作用。此外,超类中定义了一个UITapGestureRecognizer

首先,
其次,设置tapGesture.cancelsTouchesInView = false

    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(endEditing))
        tapGesture.cancelsTouchesInView = false
        view.addGestureRecognizer(tapGesture)
    }

,而不是设置
isUserInteractionEnabled = true; 在表视图中,我在单元格上设置了操作。

在 ViewDidLoad()

        super.viewDidLoad()
        
        tableView.delegate = self
    }

然后在

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell: UITableView = tableView.dequeueReusableCell(for: indexPath)
        cell.isUserInteractionEnabled = true;

如果您要创建自定义单元格,您可以尝试此解决方案。

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

    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(endEditing))
        tapGesture.cancelsTouchesInView = false
        view.addGestureRecognizer(tapGesture)
    }

Secondly, Instead of setting
isUserInteractionEnabled = true; in the table view, I set the action on the cell.

In the ViewDidLoad()

        super.viewDidLoad()
        
        tableView.delegate = self
    }

Then in the

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell: UITableView = tableView.dequeueReusableCell(for: indexPath)
        cell.isUserInteractionEnabled = true;

You can try this solution if you are creating a customized cell.

酒儿 2025-01-04 05:52:41

这对我来说很有效,你能尝试一下吗?

让点击: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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文