2011-11-29 70 views
5

我想要一個水龍頭以及雙擊一個UITableViewCell。我爲UITableview創建了一個customDataSource。雙擊UITableViewCell

我該如何做到這一點?

回答

14

正確的方式做,這是添加你UITapGestureRecognizer上的tableView:

UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)]; 
doubleTap.numberOfTapsRequired = 2; 
doubleTap.numberOfTouchesRequired = 1; 
[self.tableView addGestureRecognizer:doubleTap]; 

UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)]; 
singleTap.numberOfTapsRequired = 1; 
singleTap.numberOfTouchesRequired = 1; 
[singleTap requireGestureRecognizerToFail:doubleTap]; 
[self.tableView addGestureRecognizer:singleTap]; 

然後在你的回調方法你找到像這樣的小區:

-(void)singleTap:(UITapGestureRecognizer*)tap 
{ 
    if (UIGestureRecognizerStateEnded == tap.state) 
    { 
     CGPoint p = [tap locationInView:tap.view]; 
     NSIndexPath* indexPath = [_tableView indexPathForRowAtPoint:p]; 
     UITableViewCell* cell = [_tableView cellForRowAtIndexPath:indexPath]; 
     // Do your stuff 
    } 
} 
+2

嗨, 這個方法我試過,但每當我在一個小區翻一番TAPP,它總是進入單一的水龍頭的方法。 它永遠不會去雙擊功能。 我寫了完全相同的代碼,你已經提供。 請幫忙。 –

+0

爲了實現這個功能,您還需要禁用單元上的用戶交互。這將阻止細胞在桌面的手勢識別器有機會完成他們的工作之前進行單擊。 – rickerbh

+4

如果禁用用戶交互,您將如何滾動? –

6

我已經實現了這通過將以下內容添加到您的手勢識別器中,但我發現單擊時會有輕微的延遲。

[tapRecognizer setDelaysTouchesBegan:YES]; 
[tapRecognizer setCancelsTouchesInView:YES]; 
0

你忘了[doubleTap release];

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)]; 

doubleTap.numberOfTapsRequired = 2; 

doubleTap.numberOfTouchesRequired = 1; 

[cell addGestureRecognizer:doubleTap]; 

[doubleTap release]; 
0

您需要有這條線才能分開單擊和雙擊。 [singleTap requireGestureRecognizerToFail:doubleTap];這是示例代碼。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // After cell initialized code 
    //... 
    //... 

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)]; 
    doubleTap.numberOfTapsRequired = 2; 

    UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSingleTap:)]; 
    singleTap.numberOfTapsRequired = 1; 
    [singleTap requireGestureRecognizerToFail:doDoubleTap]; 

    [cell addGestureRecognizer:singleTap]; 
    [cell addGestureRecognizer:doubleTap]; 
    return cell; 
} 

-(void)doubleTap:(UITapGestureRecognizer*)tap 
{ 
    NSLog(@"Double Taps"); 

    CGPoint point = [tap locationInView:self.tableView]; 
    NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint: point]; 
    UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    // do your stuff. 
    // ... 
} 

-(void)singleTap:(UITapGestureRecognizer*)tap 
{ 
    NSLog(@"Single Tap"); 
    CGPoint point = [tap locationInView:self.tableView]; 
    NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:point]; 
    UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath]; 

    // Do you stuff with index path or cell here. 
    // ... 

} 
+0

這是錯的! 將2個識別器添加到viewDidLoad中的表視圖就足夠了。你正在創建2個tableview.count識別器! 如果你有很多單元格,你會記住內存和UI的響應速度, – altagir