当按钮是 UITableViewCell 内视图的子视图时,按钮在 UITableViewCell 子类中不起作用
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您添加的 UIView 实际上是一个 UIImageView。来自文档:
我认为在这种情况下, userInteractionEnabled 级联到子视图或超级视图不会将触摸传递到其子视图,因此您的按钮将不会接收触摸。将您的图像视图设置为
.userInteractionEnabled = YES
,您应该没问题。The UIView you are adding is actually a UIImageView. From the docs:
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.我遇到了同样的问题并尝试了一切。最终完成的事情是实现该方法
Just return 无论您的自定义单元格的高度是什么(查看 NIB 文件)
I had the same problem and tried EVERYTHING. The thing that finally did it was implementing the method
Just return whatever the height is of your custom cell (look in the NIB file)
最近我遇到了遗留代码的此类问题。原因是之前我们习惯将子视图添加到自定义
UITableViewCell
实例本身,现在我们必须添加到单元实例的contentView
中。我通过更改来修复它:
当然
,
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 tocontentView
of the cell instance.I fixed it by changing:
to
Of course,
isUserInteractionEnabled
should betrue
.您可以尝试使用图像来代替按钮。您可以定义单击图像时要调用的方法。
Instead of button, you could try the same with an image. You could define which method to be called when the image is clicked.