开发者

Objective C: UITable Method "didSelectRowAtIndexPath" does not work if label is added to cell

开发者 https://www.devze.com 2023-03-23 09:31 出处:网络
In my implementation, I had to add a cust开发者_高级运维om label to the cell. However, I realize that by doing so, the cell area that is covered by the custom label will not react to the user\'s \'cli

In my implementation, I had to add a cust开发者_高级运维om label to the cell. However, I realize that by doing so, the cell area that is covered by the custom label will not react to the user's 'click' and hence will not trigger the method "didSelectRowAtIndexPath".

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

[cell.contentView addSubview:questionLabel];

Is there a way to allow the whole cell area to be responsive to the user's touch even after adding a Custom label to it?


Make a custom button and put that over the lavel and onClick call the same code, see this

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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号