2013-03-16 34 views
0

我有一個UILabelUITableViewCell顯示電話號碼。當我點擊它時,我應該可以打電話給那個特定的號碼。所以爲了使它可點擊,我添加了一個UITapGestureRecognizer它。但我無法知道如何引用哪個點擊,我的意思是點擊哪個數字。tableViewCell中的UITapgestureRecogniser

- (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]]; 
} 

但是每當我看到最後一個單元格的PHONENUMBER在NSString *phoneNumber;

我該如何參考UITapGestureRecognizer中點擊哪個標籤?

+0

見實例爲什麼要使用 「'UILabel'」 而不是 「'UIButton'」? – 2013-03-16 08:22:43

+0

我試過UILabel ... – Honey 2013-03-16 08:24:52

+0

試試這個UILabel * phoneno =(UILabel *)[cell viewWithTag:[tapGestureRecognizer.view.tag]]; – 2013-03-16 08:34:36

回答

1

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

您也可以不使用標籤實現這一目標。

下面

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

我遵循第二種方式,因爲它很容易被我的代碼取代。收集它。謝謝你... – Honey 2013-03-16 08:56:06