讓UITableView響應touch事件

       咱們知道UITableView沒有像UIButton那樣能夠經過addTarget方法來監聽touch事件,所以在某些場合,特別是在UITableViewCell中包含UITextField的時候,咱們頗有可能想經過點擊UITableView的其餘地方來取消UITextField的焦點。也許有朋友會說,使用UITapGestureRecognizer手勢來取消焦點,這樣是能夠行得通,可是若是TextField中有clearButton或者其自定義的Button的時候,手勢就會吸取掉事件了,致使按鈕無效。atom

       所以,我想到的作法就是重寫UITableView的touch相關的方法,而後經過委託的方式提供給外部對象使用。首先定義Delegate:code

 

@protocol TouchTableViewDelegate <NSObject>

@optional

- (void)tableView:(UITableView *)tableView
     touchesBegan:(NSSet *)touches
        withEvent:(UIEvent *)event;

- (void)tableView:(UITableView *)tableView
 touchesCancelled:(NSSet *)touches
        withEvent:(UIEvent *)event;

- (void)tableView:(UITableView *)tableView
     touchesEnded:(NSSet *)touches
        withEvent:(UIEvent *)event;

- (void)tableView:(UITableView *)tableView
     touchesMoved:(NSSet *)touches
        withEvent:(UIEvent *)event;


@end

 

而後UITableView的子類加入一委託對象,並重寫全部touch相關方法,以下:orm

 

 

@interface  TouchTableView : UITableView
{
@private 
    id _touchDelegate;
}

@property (nonatomic,assign) id<TouchTableViewDelegate> touchDelegate;

@end

 

@implementation TouchTableView

@synthesize touchDelegate = _touchDelegate;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    
    if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] && 
        [_touchDelegate respondsToSelector:@selector(tableView:touchesBegan:withEvent:)]) 
    {
        [_touchDelegate tableView:self touchesBegan:touches withEvent:event];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
    
    if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] && 
        [_touchDelegate respondsToSelector:@selector(tableView:touchesCancelled:withEvent:)]) 
    {
        [_touchDelegate tableView:self touchesCancelled:touches withEvent:event];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    
    if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] && 
        [_touchDelegate respondsToSelector:@selector(tableView:touchesEnded:withEvent:)]) 
    {
        [_touchDelegate tableView:self touchesEnded:touches withEvent:event];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    
    if ([_touchDelegate conformsToProtocol:@protocol(TTWTableViewDelegate)] && 
        [_touchDelegate respondsToSelector:@selector(tableView:touchesMoved:withEvent:)]) 
    {
        [_touchDelegate tableView:self touchesMoved:touches withEvent:event];
    }
}

@end

 

       重寫touch方法時必須把父類實現方法寫上,不然UITableViewCell將沒法正常工做。全部的改寫工做如上所示,新的TableView類具備touch事件響應了,使用方法也很簡單在原有UITableView的基礎上賦予touchDelegate委託便可取到touch事件響應。以下:對象

 

 

- (void)loadView
{
    [super loadView];
    TouchTableView *tableView = [[TouchTableView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320, 460)   style:UITableViewStyleGrouped];
    tableView.touchDelegate = self;
    //相關處理
    [self.view addSubview:tableView];
    [tableView release];
}


- (void)tableView:(TTWTableView *)tableView
     touchesEnded:(NSSet *)touches
        withEvent:(UIEvent *)event
{
    //touch結束後的處理
}
相關文章
相關標籤/搜索