UITableViewCell在响应者链中跳过 [英] UITableViewCell skipped in responder chain

查看:133
本文介绍了UITableViewCell在响应者链中跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 UITableViewCell 的子视图中触发一个事件,然后让它冒泡响应者链并由自定义 UITableViewCell 子类。

I'm attempting to trigger an event in a subview of a UITableViewCell, and let it bubble up the responder chain and be handled by a custom UITableViewCell subclass.

基本上:

SomeView.m (这是<的一个子视图code> UITableViewCell )

[self.button addTarget:nil action:@selector(someAction:) events:UIControlEventTouchUpInside]

SomeCustomCell.m

- (void)someAction:(id)sender {
     NSLog(@"cool, the event bubbled up to the cell");
}

为了测试为什么这不起作用,我添加了< ViewController上的code> someAction:方法和ViewController最终处理从表视图单元子视图中冒泡的事件,即使Cell应该处理它。我已经检查过Cell是否在响应链上,并且我已经验证了如果它们实现了 someAction:方法。

And to test why this wasn't working, I've added the someAction: method on the ViewController and the ViewController is the one that ends up handling the event that bubbles up from the table view cell subview, even though the Cell should handle it. I've checked that the Cell is on the responder chain and I've verified that any views on the responder chain both above and below the cell will respond to the event if they implement the someAction: method.

这里到底发生了什么?

这是一个显示它的项目 https://github.com/keithnorm/ResponderChainTest 这种预期的行为是什么?我没有找到任何文档说明UITableViewCell的处理方式与其他UIResponder的处理方式不同。

Here's a project that shows it https://github.com/keithnorm/ResponderChainTest Is this expected behavior somehow? I haven't found any documentation stating UITableViewCell's are treated any differently than other UIResponder's.

推荐答案

单元格似乎询问其表格视图许可。要改变它你当然可以覆盖

The cell seems to ask its table view for permission. To change that you can of course override

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return [self respondsToSelector:action];
}

Swift 3:

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    return self.responds(to: action)
}

这篇关于UITableViewCell在响应者链中跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆