UITapGestureRecognizer

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中单击的标签?

最佳答案

使用lblphone.tag = indexPath.row + 1;代替lblphone.tag = 116;中的cellForRowAtIndexPath

您也可以不使用标签就可以实现此目的。

请参阅下面的示例

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

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

10-12 15:06