2

在我的iPhone應用程序中,我有一個消息屏幕。我在UIViewController上添加了UITapGestureRecognizer,並且屏幕上還有UITableview。我想選擇UITableViewCell,但由於UITapGestureRecognizer,我無法選擇UITableView。當我觸摸屏幕時,只調用點擊手勢動作,但不調用UITableView代表didSelectRowAtIndexPath:。任何人都可以請幫我做點擊手勢和UITableView:didSelectRowAtIndexPath:。提前致謝。如何從UITableView iphone獲取UITableView IndexPath?

+0

您的tableview是屏幕上唯一的視圖,還是它是層次結構的一部分? – 2012-08-13 13:41:46

+0

感謝您的回覆。不,tableview和UIToolbar作爲子視圖在屏幕上。 – Gopinath 2012-08-13 13:43:34

+3

行爲應該如何工作? tapgesturerecognizer應該做什麼以及什麼時候做? – 2012-08-13 13:45:19

回答

6

雖然我更喜歡馬特邁爾的建議或我使用自定義手勢識別器的另一個解決方案,不涉及自定義手勢識別器的其他解決方案的建議是讓您的手勢識別器識別您是否在桌面視圖中單擊某個單元格,如果是,則手動調用例如didSelectRowAtIndexPath

- (void)handleTap:(UITapGestureRecognizer *)sender 
{ 
    CGPoint location = [sender locationInView:self.view]; 

    if (CGRectContainsPoint([self.view convertRect:self.tableView.frame fromView:self.tableView.superview], location)) 
    { 
     CGPoint locationInTableview = [self.tableView convertPoint:location fromView:self.view]; 
     NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:locationInTableview]; 
     if (indexPath) 
      [self tableView:self.tableView didSelectRowAtIndexPath:indexPath]; 

     return; 
    } 

    // otherwise proceed with the rest of your tap handling logic 
} 

這是次優的,因爲如果你在做任何複雜的tableview操作(例如在單元格編輯,自定義控件等),你會失去這種行爲,但如果你只是想收到didSelectRowAtIndexPath,那麼這可能會完成這項工作。其他兩種方法(單獨的視圖或自定義手勢識別器)可讓您保留完整的tableview功能,但如果您只需要簡單的一些內容並且不需要tableview內置功能的其餘部分,則這種方法可行。

0

可以使用TagGesture委託:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{ 
    if ([touch.view isDescendantOfView:yourTableView]) { 
     return NO; 
    } 

    return YES; 
} 

希望這有助於。

+1

我不確定如果'touch.view'在'UIViewController'上,它就可以工作。但我認爲這個想法是正確的:你可以肯定地看看tap的位置,並將它與tableview的框架進行比較,以獲得CGPoint位置= [gestureRecognizer locationInView:self.view];'然後比較if( CGRectContainsPoint(self.tableView.frame,location))...' – Rob 2012-08-13 13:52:16

+0

@Rob我會再檢查一下。感謝您的信息。 – NSPunk 2012-08-13 13:54:17

0

一個更簡單的方法是擁有兩個視圖:一個視圖包含您想要點按手勢的視圖,另一個視圖包含桌面視圖。您可以將UITapGestureRecognizer附加到您希望它工作的視圖上,然後它不會阻止您的UITableView。

0

假設你想點擊手勢,除了在tableview中工作無處不在,你可以繼承點擊手勢識別,創造一個識別器會忽略列入excludedViews陣列的任何子視圖,防止它們產生一個成功的手勢(因此將它傳遞給didSelectRowAtIndexPath或其他):

#import <UIKit/UIGestureRecognizerSubclass.h> 

@interface MyTapGestureRecognizer : UITapGestureRecognizer 
@property (nonatomic, strong) NSMutableArray *excludedViews; 
@end 

@implementation MyTapGestureRecognizer 

@synthesize excludedViews = _excludedViews; 

- (id)initWithTarget:(id)target action:(SEL)action 
{ 
    self = [super initWithTarget:target action:action]; 
    if (self) 
    { 
     _excludedViews = [[NSMutableArray alloc] init]; 
    } 

    return self; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesBegan:touches withEvent:event]; 

    CGPoint location = [[touches anyObject] locationInView:self.view]; 

    for (UIView *excludedView in self.excludedViews) 
    { 
     CGRect frame = [self.view convertRect:excludedView.frame fromView:excludedView.superview]; 

     if (CGRectContainsPoint(frame, location)) 
      self.state = UIGestureRecognizerStateFailed; 
    } 
} 

@end 

然後,當你要使用它,只需指定要排除什麼控制:

MyTapGestureRecognizer *tap = [[MyTapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; 
[tap.excludedViews addObject:self.tableView]; 
[self.view addGestureRecognizer:tap]; 
相關問題