让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结束后的处理
}
相关文章
相关标签/搜索