uitableViewCell中的UITapgestureRecognizer

时间:2013-03-16 08:21:02

标签: iphone ios uitableview uitapgesturerecognizer

我在UILabel中有一个显示电话号码的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中查看单击的标签?

1 个答案:

答案 0 :(得分:1)

lblphone.tag = indexPath.row + 1;

中使用lblphone.tag = 116;代替cellForRowAtIndexPath

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

请参阅下面的示例

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