我有一个自定义单元格,其中有两个连接到其方法的按钮。我将像这样的单元格添加到表中:
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:相同:并且所有单元格标签或按钮都没有超出该大小。
问题是我可以选择单元格,但是无法点击按钮。好像我在敲标签一样,什么也没发生。所以我最终得到这样的东西:
编辑:只是为了澄清事情。问题不在于按钮单击方法。这是因为我无法实际单击该按钮,例如它存在某种方式,或者已被某些控件禁用。
最佳答案
你需要做以下事情
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);
}
关于ios - 表格单元格内的按钮不可单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20798475/