让UITableView响应touch事件

来源:互联网 发布:分布式更新数据 编辑:程序博客网 时间:2024/05/16 06:07

我们知道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(TouchTableViewDelegate)] &&
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事件响应。如下:

view source
print?
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}
转自http://my.oschina.net/vimfung/blog/64494
0 0
原创粉丝点击