0

我有一个显示电话号码的UILabelin 。UITableViewCell当我单击它时,我应该能够拨打该特定号码。因此,为了使其可点击,我添加了一个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

4

1 回答 1

1

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

您也可以在不使用标签的情况下实现这一点。

请参阅下面的示例

-(void)labelButton:(UITapGestureRecognizer*)tapGestureRecognizer
{
   UILabel *phoneno = (UILabel *) tapGestureRecognizer.view;
}
于 2013-03-16T08:35:22.243 回答