当按钮是 UITableViewCell 内视图的子视图时,按钮在 UITableViewCell 子类中不起作用

发布于 2025-01-05 10:36:53 字数 1165 浏览 4 评论 0原文

我在 tableView 单元格中有一个按钮,我需要响应触摸事件。 轻松解决

    [cell.playButton addTarget:self action:@selector(playButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

通常,这可以通过-tableView:cellForRowAtIndexPath: 内部

:我遇到的问题是我的按钮位于自定义 UITableViewCell 子类内部,并且也是我在该类内部创建的视图的子视图。

例如:

    UIView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    self.theView = view;
    [self addSubview:view];
    [view release];


    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    playButton.frame = CGRectMake(5, 5, 100, 30);
    [playButton setImage:[UIImage imageNamed:@"button_playvid.png"] forState:UIControlStateNormal];
    [playButton setImage:[UIImage imageNamed:@"button_playvid_active.png"] forState:UIControlStateHighlighted];
    self.playButton = playButton;
    [self.theView addSubview:playButton];

当按钮是我在自定义 UITableViewCell 中创建的视图的子视图时,单元格将突出显示,而不是按下按钮,因此我在 -tableView:cellForRowAtIndexPath 中设置的目标永远不会被调用。不知道为什么......有什么帮助吗?

谢谢,

乔尔

PS。我意识到可能没有实际的理由来创建一个完全像这样的背景视图,因为已经有一个了。这只是我用来解释我遇到的问题的示例代码..:D

感谢您的查看!

I have a button in a tableView Cell that I need to respond to touch events. Normally this would easily be solved by

    [cell.playButton addTarget:self action:@selector(playButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

inside of -tableView:cellForRowAtIndexPath:

The problem I am having is that my button is inside of a custom UITableViewCell subclass and is also the subview of a view that I am creating inside of that class..

for example:

    UIView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    self.theView = view;
    [self addSubview:view];
    [view release];


    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    playButton.frame = CGRectMake(5, 5, 100, 30);
    [playButton setImage:[UIImage imageNamed:@"button_playvid.png"] forState:UIControlStateNormal];
    [playButton setImage:[UIImage imageNamed:@"button_playvid_active.png"] forState:UIControlStateHighlighted];
    self.playButton = playButton;
    [self.theView addSubview:playButton];

When the button is a subview of a view that I create within the custom UITableViewCell then the cell is highlighted instead of the button being pressed so the target that I setup in -tableView:cellForRowAtIndexPath is never called. Not sure why.. any help?

Thanks,

Joel

PS. I realize that there probably isn't a practical reason to create a background view exactly like this since there already is one. This is just example code that I'm using to explain a problem I'm having.. :D

Thanks for Looking!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

我们只是彼此的过ke 2025-01-12 10:36:53

您添加的 UIView 实际上是一个 UIImageView。来自文档:

新的图像视图对象默认配置为忽略用户事件。如果要处理 UIImageView 的自定义子类中的事件,则必须在初始化对象后显式将 userInteractionEnabled 属性的值更改为 YES。

我认为在这种情况下, userInteractionEnabled 级联到子视图或超级视图不会将触摸传递到其子视图,因此您的按钮将不会接收触摸。将您的图像视图设置为 .userInteractionEnabled = YES ,您应该没问题。

The UIView you are adding is actually a UIImageView. From the docs:

New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.

I think that either userInteractionEnabled cascades to subviews or the superview will not pass the touch to its subviews in this case, so your button will not be receiving touches. Set your image view to have .userInteractionEnabled = YES and you should be fine.

一个人练习一个人 2025-01-12 10:36:53

我遇到了同样的问题并尝试了一切。最终完成的事情是实现该方法

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Just return 无论您的自定义单元格的高度是什么(查看 NIB 文件)

I had the same problem and tried EVERYTHING. The thing that finally did it was implementing the method

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Just return whatever the height is of your custom cell (look in the NIB file)

反差帅 2025-01-12 10:36:53

最近我遇到了遗留代码的此类问题。原因是之前我们习惯将子视图添加到自定义 UITableViewCell 实例本身,现在我们必须添加到单元实例的 contentView 中。

我通过更改来修复它:

addSubview(increaseButton)

当然

contentView.addSubview(increaseButton)

isUserInteractionEnabled 应该是true

Recently I've ran into such issue with legacy code. The reason was that earlier we used to add subviews to custom UITableViewCell instance itself and now we have to add to contentView of the cell instance.

I fixed it by changing:

addSubview(increaseButton)

to

contentView.addSubview(increaseButton)

Of course, isUserInteractionEnabled should be true.

猫性小仙女 2025-01-12 10:36:53

您可以尝试使用图像来代替按钮。您可以定义单击图像时要调用的方法。

Instead of button, you could try the same with an image. You could define which method to be called when the image is clicked.

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