gpt4 book ai didi

iphone - tableViewCell 中的 UITapgestureRecogniser

转载 作者:行者123 更新时间:2023-11-29 04:04:35 25 4
gpt4 key购买 nike

我在 UITableViewCell 中有一个 UILabel,它显示电话号码。当我单击它时,我应该能够调用该特定号码。因此,为了使其可点击,我向其中添加了一个 UITapGestureRecognizer 。但我不知道如何引用单击了哪个水龙头,我的意思是单击了哪个数字。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
lblphone = [[UILabel alloc] initWithFrame:CGSizeMake(20,30,50,100)];
lblphone.tag = 116;
lblphone.backgroundColor = [UIColor clearColor];
lblphone.text=[NSString stringwithFormat:@"%@",phone];
[lblphone setFont:[UIFont fontWithName:@"Helvetica" size:12]];
[lblphone setLineBreakMode:UILineBreakModeWordWrap];
[lblphone setUserInteractionEnabled:YES];

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelButton:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[lblphone addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
[cell addSubview:lblphone];
}

-(IBAction)labelButton:(UITapGestureRecognizer*)tapGestureRecognizer
{
NSIndexPath *indexPath;
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UILabel *phoneno = (UILabel*)[cell viewWithTag:116];
NSString *phoneNumber = [@"telprompt://" stringByAppendingString:phoneno.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
}

但每次我都能看到 NSString *phoneNumber; 中最后一个单元格的电话号码;

如何引用 UITapGestureRecognizer 中单击的标签?

最佳答案

cellForRowAtIndexPath中使用lblphone.tag = indexPath.row + 1;而不是lblphone.tag = 116;

您也可以在不使用标签的情况下实现此目的。

参见下面的示例

-(void)labelButton:(UITapGestureRecognizer*)tapGestureRecognizer
{
UILabel *phoneno = (UILabel *) tapGestureRecognizer.view;
}

关于iphone - tableViewCell 中的 UITapgestureRecogniser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15447149/

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