目标C:UITable方法如果将标签添加到单元格,则“didSelectRowAtIndexPath”不起作用

时间:2011-07-26 04:27:15

标签: objective-c ios uitableview uilabel didselectrowatindexpath

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

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

[cell.contentView addSubview:questionLabel];

是否有办法让整个手机区域即使在为其添加自定义标签后也能响应用户的触摸?

1 个答案:

答案 0 :(得分: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];
}