0

電話番号を表示するがありますUILabelUITableViewCellクリックすると、その特定の番号に電話をかけることができるはずです。クリック可能にするために、を追加しました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 に答える