0

在我的实现中,我必须向单元格添加自定义标签。但是,我意识到这样做,自定义标签覆盖的单元格区域不会对用户的“点击”做出反应,因此不会触发方法“didSelectRowAtIndexPath”。

CustomOHAttributLabel *questionLabel = [[CustomOHAttributLabel alloc]initWithFrame:CGRectMake(10, (60-labelHeight)/2, 280, labelHeight)];        

[cell.contentView addSubview:questionLabel];

有没有办法让整个单元格区域即使在添加自定义标签后也能响应用户的触摸?

4

1 回答 1

2

制作一个自定义按钮并将其放在 lavel 和 onClick 调用相同的代码,请参阅此

CustomOHAttributLabel *questionLabel = [[CustomOHAttributLabel alloc]initWithFrame:CGRectMake(10, (60-labelHeight)/2, 280, labelHeight)];

UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(10, (60-labelHeight)/2, 280, labelHeight);
    [btn addTarget:self action:@selector(doSomeThing) forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:questionLabel];
[cell.contentView addSubview:btn];



-(void)doSomeThing
{
  //write code which you write in didSelectRowAtIndexPath method
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   [self doSomeThing];
}
于 2011-07-26T05:25:30.507 回答