2011-11-29 31 views
5

Tôi muốn nhận một lần nhấn cũng như nhấn đúp vào UITableViewCell. Tôi đã tạo một customDataSource cho UITableview.Nhấn đúp vào UITableViewCell

Tôi làm cách nào để đạt được điều này?

Trả lời

14

Các cách chính xác để làm điều này là để thêm UITapGestureRecognizer của bạn trên 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]; 

Sau đó, trong các phương thức callback của bạn, bạn tìm các tế bào với một cái gì đó như thế này:

-(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

Xin chào, Tôi đã thử phương pháp này, nhưng bất cứ khi nào tôi nhấn đúp vào một ô, nó luôn đi vào phương pháp của vòi duy nhất. Nó không bao giờ chuyển sang chức năng Nhấn đúp. Tôi đã viết chính xác mã mà bạn đã cung cấp. vui lòng trợ giúp. –

+0

Để làm việc này, bạn cũng cần vô hiệu hóa tương tác người dùng trên ô. Điều này sẽ ngăn chặn các tế bào ăn các vòi duy nhất trước khi nhận dạng cử chỉ của bàn có được một cơ hội để làm công việc của họ. – rickerbh

+4

Nếu bạn vô hiệu hóa tương tác của người dùng, bạn cuộn như thế nào? –

6

tôi đã đạt được bằng cách thêm phần sau vào trình nhận dạng cử chỉ của bạn, mặc dù tôi thấy rằng có một chút chậm trễ khi nhấn một lần.

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

bạn đã quên [doubleTap release];

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

doubleTap.numberOfTapsRequired = 2; 

doubleTap.numberOfTouchesRequired = 1; 

[cell addGestureRecognizer:doubleTap]; 

[doubleTap release]; 
0

Bạn cần có dòng này để tách biệt giữa các vòi đơn và đôi. [singleTap requireGestureRecognizerToFail: doubleTap]; Đây là mã ví dụ.

- (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

Điều này là sai! Thêm 2 trình nhận dạng vào chế độ xem bảng trong viewDidLoad là đủ. bạn đang tạo 2 bộ nhận dạng tableview.count! Nếu bạn có nhiều ô, bạn nhớ bộ nhớ và giao diện người dùng không có gì ... – altagir

Các vấn đề liên quan