简体   繁体   English

表格单元格内的按钮不可单击

[英]Button inside table cell not clickable

I have a custom cell which has two buttons that are connected to it's methods. 我有一个自定义单元格,其中有两个连接到其方法的按钮。 I add the cell to the table like this: 我将像这样的单元格添加到表中:

BSOrderedItemCell *cell = (BSOrderedItemCell *)[tableView dequeueReusableCellWithIdentifier:@"OrderItemCell"];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"OrderItemCell" owner:self options:nil];
    cell = (BSOrderedItemCell *)[nib objectAtIndex:0];
}

And after that I set it's properties. 之后,我设置它的属性。

The cell size is the same as table's heightForRowAtIndexPath: and none of the cells labels or buttons are outside that size. 单元格的大小与表的heightForRowAtIndexPath相同:并且所有单元格标签或按钮都没有超出该大小。

The problem is that I can select the cell, but I can't tap on the buttons. 问题是我可以选择单元格,但是无法点击按钮。 It's as if I'm tapping on a label, nothing happens. 就像我在敲标签一样,什么也没发生。 So I end up with something like this: 所以我最终得到这样的东西: UITableView的屏幕截图

EDIT: Just to clarify things. 编辑:只是为了澄清事情。 The problem is not the button click methods. 问题不在于按钮单击方法。 It's that I can't physically click the button, like there is something in the way of it, or it's disabled by some control. 这是因为我无法实际单击该按钮,例如它存在某种方式,或者已被某些控件禁用。

you need to do following things 你需要做以下事情

  1. Set the tag of your button in yourcellxib. 在yourcellxib中设置按钮的标签。 orderitemcell in your case 您的情况下的orderitemcell
  2. get the button 取得按钮

    UIButton *btn =(UIButton*)[cell viewWithTag:]; UIButton * btn =(UIButton *)[cell viewWithTag:]; btn.tag=indexPath.row; btn.tag = indexPath.row;

3.add method to your button 3.添加方法到您的按钮

[btn addTarget:self action:@selector(buttonClicked:)  forControlEvents:UIControlEventTouchUpInside];

now make a method 现在做一个方法

-(void)buttonClicked:(UIButton*)btn
{
NSLog(@"the button clicked is on cell number%d",btn.tag);

}

You can try this.. 你可以试试看

[cell.button addTarget:self action:@selector(buttonClicked:)  forControlEvents:UIControlEventTouchUpInside];

To avoid cell selection: 为了避免选择单元格:

Cell's xib -> Selection -> Set as None

Do not create IBOutlot for then in you cellForRowAtIndexpath add the method to button 不要为它创建IBOutlot,然后在cellForRowAtIndexpath中将方法添加到按钮

[cell.button addTarget:self action:@selector(buttonClicked:)  forControlEvents:UIControlEventTouchUpInside];

and perform the selector there and if you do not required row did select delegate then add 并在那里执行选择器,如果您不需要行,请选择委托,然后添加

cell.userInteractionEnabled = NO; //too //太

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM