让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> |
05 | - ( void )tableView:(UITableView *)tableView |
06 | touchesBegan:(NSSet *)touches |
07 | withEvent:(UIEvent *)event; |
09 | - ( void )tableView:(UITableView *)tableView |
10 | touchesCancelled:(NSSet *)touches |
11 | withEvent:(UIEvent *)event; |
13 | - ( void )tableView:(UITableView *)tableView |
14 | touchesEnded:(NSSet *)touches |
15 | withEvent:(UIEvent *)event; |
17 | - ( void )tableView:(UITableView *)tableView |
18 | touchesMoved:(NSSet *)touches |
19 | withEvent:(UIEvent *)event; |
然后UITableView的子类加入一委托对象,并重写所有touch相关方法,如下:
1 | @interface TouchTableView : UITableView |
7 | @property (nonatomic,assign) id<TouchTableViewDelegate> touchDelegate; |
01 | @implementation TouchTableView |
03 | @synthesize touchDelegate = _touchDelegate; |
05 | - ( void )touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event |
07 | [super touchesBegan:touches withEvent:event]; |
09 | if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] && |
10 | [_touchDelegate respondsToSelector:@selector(tableView:touchesBegan:withEvent:)]) |
12 | [_touchDelegate tableView:self touchesBegan:touches withEvent:event]; |
16 | - ( void )touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event |
18 | [super touchesCancelled:touches withEvent:event]; |
20 | if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] && |
21 | [_touchDelegate respondsToSelector:@selector(tableView:touchesCancelled:withEvent:)]) |
23 | [_touchDelegate tableView:self touchesCancelled:touches withEvent:event]; |
27 | - ( void )touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event |
29 | [super touchesEnded:touches withEvent:event]; |
31 | if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] && |
32 | [_touchDelegate respondsToSelector:@selector(tableView:touchesEnded:withEvent:)]) |
34 | [_touchDelegate tableView:self touchesEnded:touches withEvent:event]; |
38 | - ( void )touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event |
40 | [super touchesMoved:touches withEvent:event]; |
42 | if ([_touchDelegate conformsToProtocol:@protocol(TTWTableViewDelegate)] && |
43 | [_touchDelegate respondsToSelector:@selector(tableView:touchesMoved:withEvent:)]) |
45 | [_touchDelegate tableView:self touchesMoved:touches withEvent:event]; |
重写touch方法时必须把父类实现方法写上,否则UITableViewCell将无法正常工作。所有的改写工作如上所示,新的TableView类具有touch事件响应了,使用方法也很简单在原有UITableView的基础上赋予touchDelegate委托即可取到touch事件响应。如下:
04 | TouchTableView *tableView = [[TouchTableView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320, 460) style:UITableViewStyleGrouped]; |
05 | tableView.touchDelegate = self; |
07 | [self.view addSubview:tableView]; |
12 | - ( void )tableView:(TTWTableView *)tableView |
13 | touchesEnded:(NSSet *)touches |
14 | withEvent:(UIEvent *)event |