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

时间:2013-12-27 09:52:32

标签: ios objective-c uitableview xcode5

我有一个自定义单元格,它有两个连接到它的方法的按钮。我将单元格添加到表格中:

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

然后我设置它的属性。

单元格大小与表格的heightForRowAtIndexPath相同:并且没有单元格标签或按钮超出该大小。

问题是我可以选择单元格,但我无法点击按钮。就好像我正在点击一个标签,没有任何反应。所以我最终得到这样的结果:Screenshot of UITableView

编辑:只是为了澄清事情。问题不在于按钮点击方法。这是我无法在物理上点击按钮,就像它有某种方式,或者被某些控件禁用。

3 个答案:

答案 0 :(得分:0)

你需要做以下事情

  1. 在您的cellxib中设置按钮的标记。在你的情况下orderitemcell
  2. 获取按钮

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

  3. 3.添加按钮的方法

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

    现在制作方法

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

答案 1 :(得分:0)

你可以试试这个..

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

为避免细胞选择:

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

答案 2 :(得分:-2)

不要为你创建IBOutlot cellForRowAtIndexpath将方法添加到按钮

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

并在那里执行选择器,如果你不需要行,请选择委托,然后添加

cell.userInteractionEnabled = NO; //也是

相关问题