让UITableView响应touch事件

让UITableView响应touch事件

5人收藏此文章, 我要收藏发表于1年前(2012-07-01 17:49) , 已有 2810次阅读 ,共 0个评论

       我们知道UITableView没有像UIButton那样可以通过addTarget方法来监听touch事件,因此在某些场合,特别是在UITableViewCell中包含UITextField的时候,我们很有可能想通过点击UITableView的其他地方来取消UITextField的焦点。也许有朋友会说,使用UITapGestureRecognizer手势来取消焦点,这样是可以行得通,但是如果TextField中有clearButton或者其自定义的Button的时候,手势就会吸收掉事件了,导致按钮无效。

       因此,我想到的做法就是重写UITableView的touch相关的方法,然后通过委托的方式提供给外部对象使用。首先定义Delegate:

01 @protocol TouchTableViewDelegate <NSObject>
02  
03 @optional
04  
05 - (void)tableView:(UITableView *)tableView
06      touchesBegan:(NSSet *)touches
07         withEvent:(UIEvent *)event;
08  
09 - (void)tableView:(UITableView *)tableView
10  touchesCancelled:(NSSet *)touches
11         withEvent:(UIEvent *)event;
12  
13 - (void)tableView:(UITableView *)tableView
14      touchesEnded:(NSSet *)touches
15         withEvent:(UIEvent *)event;
16  
17 - (void)tableView:(UITableView *)tableView
18      touchesMoved:(NSSet *)touches
19         withEvent:(UIEvent *)event;
20  
21  
22 @end

然后UITableView的子类加入一委托对象,并重写所有touch相关方法,如下:

1 @interface  TouchTableView : UITableView
2 {
3 @private
4     id _touchDelegate;
5 }
6  
7 @property (nonatomic,assign) id<TouchTableViewDelegate> touchDelegate;
8  
9 @end

01 @implementation TouchTableView
02  
03 @synthesize touchDelegate = _touchDelegate;
04  
05 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
06 {
07     [super touchesBegan:touches withEvent:event];
08      
09     if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] &&
10         [_touchDelegate respondsToSelector:@selector(tableView:touchesBegan:withEvent:)])
11     {
12         [_touchDelegate tableView:self touchesBegan:touches withEvent:event];
13     }
14 }
15  
16 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
17 {
18     [super touchesCancelled:touches withEvent:event];
19      
20     if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] &&
21         [_touchDelegate respondsToSelector:@selector(tableView:touchesCancelled:withEvent:)])
22     {
23         [_touchDelegate tableView:self touchesCancelled:touches withEvent:event];
24     }
25 }
26  
27 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
28 {
29     [super touchesEnded:touches withEvent:event];
30      
31     if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] &&
32         [_touchDelegate respondsToSelector:@selector(tableView:touchesEnded:withEvent:)])
33     {
34         [_touchDelegate tableView:self touchesEnded:touches withEvent:event];
35     }
36 }
37  
38 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
39 {
40     [super touchesMoved:touches withEvent:event];
41      
42     if ([_touchDelegate conformsToProtocol:@protocol(TTWTableViewDelegate)] &&
43         [_touchDelegate respondsToSelector:@selector(tableView:touchesMoved:withEvent:)])
44     {
45         [_touchDelegate tableView:self touchesMoved:touches withEvent:event];
46     }
47 }
48  
49 @end

       重写touch方法时必须把父类实现方法写上,否则UITableViewCell将无法正常工作。所有的改写工作如上所示,新的TableView类具有touch事件响应了,使用方法也很简单在原有UITableView的基础上赋予touchDelegate委托即可取到touch事件响应。如下:

01 - (void)loadView
02 {
03     [super loadView];
04     TouchTableView *tableView = [[TouchTableView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320, 460)   style:UITableViewStyleGrouped];
05     tableView.touchDelegate = self;
06     //相关处理
07     [self.view addSubview:tableView];
08     [tableView release];
09 }
10  
11  
12 - (void)tableView:(TTWTableView *)tableView
13      touchesEnded:(NSSet *)touches
14         withEvent:(UIEvent *)event
15 {
16     //touch结束后的处理
17 }

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值