我有一个自定义单元格,它有两个连接到它的方法的按钮。我将单元格添加到表格中:
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相同:并且没有单元格标签或按钮超出该大小。
问题是我可以选择单元格,但我无法点击按钮。就好像我正在点击一个标签,没有任何反应。所以我最终得到这样的结果:
编辑:只是为了澄清事情。问题不在于按钮点击方法。这是我无法在物理上点击按钮,就像它有某种方式,或者被某些控件禁用。
答案 0 :(得分:0)
你需要做以下事情
获取按钮
UIButton * btn =(UIButton *)[cell viewWithTag:]; btn.tag = indexPath.row;
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;
//也是