2012-04-09 117 views
19

我正在使用NIB文件來佈置自定義表格視圖單元格。這個單元格帶有一個名爲lblName的標籤。將UITapGestureRecognizer添加到此標籤永遠不會觸發關聯的事件。我有userInteractionEnabled = YES。如何將UITapGestureRecognizer添加到表格視圖單元格內的UILabel中?

我猜測問題是UILabel在TableView中,並且表格/單元格視圖正在攔截水龍頭。我能做些什麼嗎?

我想要做的就是在按下UILabel時執行一些自定義操作!我所見過的所有解決方案都很荒謬。使用標準工具集應該很容易。但顯然不是。

下面是我使用的代碼:

- (void)tapAction { 
    NSLog(@"Tap action"); 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib 

    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]; 
    [recognizer setNumberOfTapsRequired:1]; 
    //lblName.userInteractionEnabled = true; (setting this in Interface Builder) 
    [lblName addGestureRecognizer:recognizer]; 
} 

回答

20

EASY WAY創建的UILabel :

您也可以使用隱形b utton在該標籤的頂部。所以它會減少您爲該標籤添加tapGesture的工作。

另一種方式:

你不應該爲UILabel創建一個IBOutlet。當你這樣做時,你會在自定義類實現文件中添加一個插座。您無法在其他文件中訪問。因此,在自定義類IB中爲該標籤設置標籤,並在cellForRowAtIndexPath:方法中編寫代碼。

更新:

cellForRowAtIndexPath:方法,

for(UIView *view in cell.contentViews.subviews) { 
    if(view.tag == 1) { 
     UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]; 
     [tap setNumberOfTapsRequired:1]; 
     [view addGestureRecognizer:tap]; 
    } 
} 
+0

謝謝,這個伎倆!在你的代碼中修正一些錯別字:首先「if」應當是「for」,然後你想枚舉cell.contentViews.subviews。感謝你的回答! – Bryan 2012-04-10 19:11:34

+0

哦,我在這裏輸入了自己的名字。所以有一點小錯誤,我會更新它。 – 2012-04-11 04:41:53

+3

直到我在單元本身設置了「用戶交互啓用」後,這才起作用 – 2014-08-05 15:16:25

9

這樣做的工作沒有問題:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 
    // create you cell 
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]; 
    [lbl setText:@"example"]; 
    [lbl setUserInteractionEnabled:YES]; 
    [cell.contentView addSubview:lbl]; 
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]; 
    tap.tag = [NSIndexPath row]; 
    [tap setNumberOfTapsRequired:1]; 
    [lbl addGestureRecognizer:tap]; 
    ... 
} 

- (void)tapAction:(id)sender { 
    switch(((UITapGestureRecognizer *)sender).view.tag) { 
    case 0: 
      // code 
      break; 
    case 1: 
     // code 
     break; 
     .... 
    } 
} 

即使在情況下,與IB

+0

這將幫助他。但是他需要用Tap手勢在UITableViewCell內創建一個標籤。 – 2012-04-10 10:34:03

+0

然後在創建單元格的方法中,當您創建UITapGestureRecognizer時,爲其添加標籤並管理處理程序中用於區分標籤的事件。 – WhiteTiger 2012-04-10 12:18:21

+0

你能解釋一下代碼嗎? – 2012-04-10 12:46:21

0

方式的Dinesh建議的for循環使用屬性變量而不將工作。

UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]; 
[tap setNumberOfTapsRequired:1]; 
[self.myUILabel addGestureRecognizer:tap]; 
12
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]; 
[recognizer setNumberOfTapsRequired:1]; 
lblName.userInteractionEnabled = YES; 
[lblName addGestureRecognizer:recognizer]; 
+1

需要將userInteractionEnabledProperty設置爲YES。在我的情況下,標籤是在另一個視圖內,沒有這個,手勢不起作用。 – alcamla 2015-12-17 23:14:09

0

一旦指定點擊手勢到的UILabel和用戶交互設置爲啓用,在你的回調函數,你可以你可以添加在你的細胞的-awakeFromNib方法

UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizerAction:)]; 
[self.yourLabel setUserInteractionEnabled:YES]; 
[self.yourLabel addGestureRecognizer:gesture]; 
1

下一個從單元格視圖中查找索引路徑,但在超級視圖中搜索:

- (UITableViewCell *) findCellInSuperview:(UIView *)view 
{ 
UITableViewCell *cell = nil; 

    NSString *className = NSStringFromClass([[view superview] class]); 
    if ([className isEqualToString:@"UITableViewCell"]) { 
     cell = (UITableViewCell *)[view superview]; 
    } else { 
     if ([view superview] != nil) { 
      cell = [self findCellInSuperview:[view superview]]; 
     } 
    } 

return cell; 
} 
0

對於Swift , 你可以在你的cellForRowAtIndexPath方法中添加這個。

var tap = UITapGestureRecognizer(target: self, action: "labelTapped") 
tap.numberOfTapsRequired = 1 
cell.label.addGestureRecognizer(tap) 
cell.label.tag = indexPath.row 

然後,對於動作

func labelTapped(gesture: UITapGestureRecognizer) { 
    let indexPath = NSIndexPath(forRow: gesture.view!.tag, inSection: 0) 
    let cell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell 

    // Do whatever you want. 
} 
+1

OP詢問Objective-C問題,而不是Swift。 – 2015-05-09 14:04:42

+1

你有什麼會導致運行時錯誤。 'action:「labelTapped」'說這個方法期望在沒有任何參數的情況下被調用,但是這個方法的簽名表明否則。 – maml 2015-06-24 00:42:43

+0

表格視圖單元格是**重用**;是否存在將手勢識別器多次添加到相同單元的風險? (同樣的問題也適用於包含在單元格內的按鈕中的「addTarget:action:forControlEvents:')。 – 2016-11-25 05:13:23

4

可以使用以下代碼來對UILable添加敲擊手勢中的UITableView細胞

UITapGestureRecognizer *lblAction = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lblClick:)]; 
lblAction.delegate =self; 
lblAction.numberOfTapsRequired = 1; 
cell.lbl.userInteractionEnabled = YES; 
[cell.lbl addGestureRecognizer:lblAction]; 

和訪問選擇方法

- (void)lblClick:(UITapGestureRecognizer *)tapGesture { 
    UILabel *label = (UILabel *)tapGesture.view; 
    NSLog(@"Lable tag is ::%ld",(long)label.tag); 
} 

對於斯威夫特

let tapGesture : UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(lblClick(tapGesture:))) 
tapGesture.delegate = self 
tapGesture.numberOfTapsRequired = 1 
cell.lbl.userInteractionEnabled = true 
cell.lbl.tag = indexPath.row 
cell.lbl.addGestureRecognizer(tapGesture) 

func lblClick(tapGesture:UITapGestureRecognizer){ 
    print("Lable tag is:\(tapGesture.view!.tag)") 
} 
0

對於斯威夫特3

let tapGesture : UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: 
#selector(lblClick(tapGesture:))) 
tapGesture.delegate = self 
tapGesture.numberOfTapsRequired = 1 
cell.lbl.isUserInteractionEnabled = true 
cell.lbl.tag = indexPath.row 
cell.lbl.addGestureRecognizer(tapGesture) 

然後

func lblClick(tapGesture:UITapGestureRecognizer){ 
    print("Lable tag is:\(tapGesture.view!.tag)") 
} 
相關問題