gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:14:58 24 4
gpt4 key购买 nike

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

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

[cell.contentView addSubview:questionLabel];

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

最佳答案

制作一个自定义按钮并将其放在 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];
}

关于objective-c - Objective-C : UITable Method "didSelectRowAtIndexPath" does not work if label is added to cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6825260/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com