2012-01-13 39 views
0

我在表格的每個單元格中都有3個UILabels。我已經爲每個人添加了點擊手勢識別器,但是當它被點擊時,我怎樣才能獲取被點擊的行的索引路徑?如何在UITableView中獲取UILabel動作?

這裏是我的代碼

UILabel *tit_lbl=[[UILabel alloc]initWithFrame:CGRectMake(10,10,280,45)]; 
[tit_lbl setText:[[final_dictionaires_array objectAtIndex:indexPath.row] objectForKey:title]]; 
[tit_lbl setUserInteractionEnabled:YES]; 
[tit_lbl setTag:indexPath.row]; 
[btn addSubview:tit_lbl]; 
[tit_lbl release]; 

UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToArticleDetailsView:)]; 
[tit_lbl addGestureRecognizer:tapRecognizer]; 
[tapRecognizer release]; 

有了這個,我可以得到標籤行動,但我想被點擊哪一行哪個標籤? (每行有3個標籤)。

+0

這裏是我的代碼UILabel * tit_lbl = [[UILabel alloc] initWithFrame:CGRectMake(10,10,280,45)]; [tit_lbl setText:[[final_dictionaires_array objectAtIndex:indexPath.row] objectForKey:title]]; [tit_lbl setUserInteractionEnabled:YES]; [tit_lbl setTag:indexPath.row]; [btn addSubview:tit_lbl]; [tit_lbl release]; UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToArticleDetailsView :)]; [tit_lbl addGestureRecognizer:tapRecognizer]; [tapRecognizer發佈]; – 2012-01-13 12:20:52

+0

爲什麼不使用UIButtons自定義樣式? – Novarg 2012-01-13 12:41:47

回答

1

我與使用按鈕自定義樣式,而不是基本上重現使用標籤和手勢識別器按鈕的建議達成一致。但是,你仍然有同樣的問題。哪個標籤被挖掘?桌子上有哪排呢?

通過爲每個標籤分配標籤然後查詢手勢識別器的view屬性,您可以找出哪個標籤被點擊。手勢識別器會在您的goToArticleDetailsView方法的參數:

-(void)goToArticleDetailsView:(id)sender 
{ 
    UITapGestureRecognizer *tapGR = (UITapGestureRecognizer*)sender; 
    if (tapGR.view.tag == 1) 
     // tapGR.view is the label that the gesture recognize was attached to 

你可以找出哪些表的行用手勢識別器的locationOfTouch方法挖掘,再加上的UITableView的indexPathForRowAtPoint:方法:

CGPoint touchLocation = [tapGR locationOfTouch:0 inView:self.tableView]; 
NSIndexPath *tappedRow = [self.tableView indexPathForRowAtPoint:touchLocation]; 
-1

無法點擊UILabel。如果您想讓用戶與值交互,您應該使用UITextfield。

要獲取該操作,您應該向UITextField的實例發送消息。

爲了得到正確的電池可以使用「indexPath」

好運

+1

你可以使用UIGestureRecogniser對UILabel進行操作。但在我的情況下,我想要檢測哪個lebel和哪一行點擊了? – 2012-01-13 12:17:04

+0

您可以擁有一個GestureRecognizer。但使用標籤知道哪一行被點擊。如果我沒有錯,TapGestureRecognizer沒有標籤成員變量。所以嘗試使用標記變量對UITapGestureRecognizer進行子類化。 – Amal 2012-03-14 13:20:51

相關問題