2013-04-18 46 views
0

我定製了tableview單元格,我設置了一個時間標籤&我需要什麼,如果我觸摸/單擊標籤它顯示該標籤的文本。獲取標籤後的文本觸摸它[tableView]

這裏是時間標籤集:

cell.time.text = @"5 mins ago"; 
cell.time.tag = 1; 
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]; 
[cell.time setUserInteractionEnabled:YES]; 
[cell.time addGestureRecognizer:tap]; 

所以,當我拍了拍標籤必須做到以下事先

-(void)tapAction:(id)sender 
{ 
    // I want to get the text of that label for above example must return 

    // 5 mins ago 
} 

謝謝..

+0

有啥問題? – Rajneesh071

+2

做一件事兄弟。使用自定義按鈕而不是標籤。然後應用邏輯即首先獲取單元格,然後從單元格中獲取按鈕。然後詢問按鈕的標題。 – Tendulkar

+0

是你的問題解決? – Rajneesh071

回答

1

您可以訪問水龍頭查看

-(void)tapAction:(id)sender 
{ 
    UITapGestureRecognizer *tapRec = (UITapGestureRecognizer *)sender; 
    UILabel *label = (UILabel *)tapRec.view; 
    NSLog(@"%@",label.text); 
} 
+0

非常感謝..這就是我要找的。 – SimpleojbC

+0

您的歡迎夥伴.. @ user2104662 – Rajneesh071

0

你爲什麼不使用而不是在你的單元格上設置一個gesturerecognizer委託方法?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

} 

然後,您可以從indexPath中提取該行,並知道選擇了哪個單元格。

0

使用下面的代碼

cell.time.text = @"5 mins ago"; 
cell.time.tag = [indexpath.row]; 
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]; 
[cell.time setUserInteractionEnabled:YES]; 
[cell.time addGestureRecognizer:tap]; 


-(void)tapAction:(id)sender 
{ 
UIbutton *btn = (UIbutton *)sender 
Nsstring *str = [btn.text] 
// I want to get the text of that label for above example must return 

// 5 mins ago 
} 
0

試試這個

-(void)tapAction:(id)sender 
{ 
    UITapGestureRecognizer *recognizer = (UITapGestureRecognizer *)sender; 
    UILabel *label = (UILabel *)recognizer.view; 
    NSString *text = label.text; 
} 
+0

@Downvoter你能解釋downvote的原因嗎? – Anupdas