2012-07-24 26 views
0

我在iPad上的表格视图单元格中放置按钮时出现了一个奇怪的问题。按钮在单元格上没有响应在iPad上

这里是为了更好地理解两个截图(该按钮位于右侧,有一个白色的背景色):

iPhone

iPad

与iPhone没有问题,但对iPad这个按钮没有响应。 定位似乎是正确的,因为它被正确绘制。

这是我的自定义代码的UITableViewCell:

rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[rightButton setContentMode:UIViewContentModeCenter]; 
[rightButton setBackgroundColor:[UIColor whiteColor]]; 
int xPos = buttonWidth-rightButton.frame.size.width; 
[rightButton setFrame:CGRectMake(xPos, 0, buttonHeight, buttonHeight)]; 
[self.contentView addSubview:rightButton]; 

//buttonWidth = 310, buttonHeight = 60 

图像和目标是通过另一个类设置:

[cell.rightButton setImage:[UIImage imageNamed:@"options.png"] forState:UIControlStateNormal]; 
[cell.rightButton addTarget:self action:@selector(showOptions:) forControlEvents:UIControlEventTouchUpInside]; 

我也试过这种把所有的元素到小区后

[self.contentView bringSubviewToFront: rightButton]; 

确保按钮位于最上方。

没有工作。

对此的任何想法?


我还找不到解决方案。

我在此期间所做的:

  • 显现放置在细胞中的所有对象上的所有其他细胞对象

任何其他的想法(显示不必要的重叠)

  • 禁用的用户交互?

  • +0

    向我们展示了如何将UIButton添加到UITableViewCell以及如何为UIButton设置动作。 – holex 2012-07-24 14:36:14

    +0

    您可以提供更多细节,或者编写按钮创建代码。 – Nilesh 2012-07-24 14:36:41

    +0

    用代码更新了我的帖子。 – 2012-07-24 14:48:35

    回答

    0

    第一次尝试这样的:

    rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [rightButton setContentMode:UIViewContentModeCenter]; 
    [rightButton setBackgroundColor:[UIColor whiteColor]]; 
    int xPos = buttonWidth-rightButton.frame.size.width; 
    [rightButton setFrame:CGRectMake(xPos, 0, buttonHeight, buttonHeight)]; 
    [self.contentView addSubview:rightButton]; 
    [self.contentView bringSubviewToFront: rightButton] 
    [self.contentView bringSubviewToFront: rightButton]; 
    rightButton.userInteractionEnabled = YES; 
    
    [cell.rightButton setImage:[UIImage imageNamed:@"options.png"] forState:UIControlStateNormal]; 
    [cell.rightButton addTarget:self action:@selector(showOptions:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.rightButton setUserInteractionEnabled:YES]; 
    

    或者你也可以添加一个点触手势你的按钮,它会解决这个问题: 不是这一行的,

    [cell.rightButton addTarget:self action:@selector(showOptions:) forControlEvents:UIControlEventTouchUpInside]; 
    

    写这,

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showOptions:)]; 
    cell.rightButton addGestureRecognizer:tap]; 
    [cell.rightButton setUserInteractionEnabled:YES]; 
    
    +0

    我尝试了所有的建议。但iPad仍然没有回应。 – 2012-07-25 06:27:32

    0

    好吧,我找到索姆东西:

    对于iPad来说,按钮只有在水平位于前320个像素之间时才会响应。

    这里是触摸按钮(远放置在右侧),当调试这样说:

    (lldb) po touches 
    (NSSet *) $1 = 0x08249cd0 {(
    <UITouch: 0x82c63d0> phase: Began tap count: 1 window: <UIWindow: 0x8266ca0; 
    frame = (0 0; 768 1024); opaque = NO; autoresize = RM+BM; 
    layer = <UIWindowLayer: 0x8266d60>> view: <SBMultiCell: 0x82cb8a0; baseClass = UITableViewCell; 
    frame = (0 0; 744 93); autoresize = W; layer = <CALayer: 0x82cb830>> 
    location in window: {713, 123} previous location in window: {713, 123} 
    location in view: {701, 44} previous location in view: {701, 44} 
    )} 
    
    (lldb) po rightButton 
    (UIButton *) $2 = 0x082cd500 <UIButton: 0x82cd500; 
    frame = (658 0; 86 86); opaque = NO; layer = <CALayer: 0x82cd5e0>> 
    

    的触摸位置恰恰是在按钮尺寸之间也一切都正确绘制。正如我之前写的,没有重叠的元素或类似的东西...

    相关问题