让UITableView响应touch事件
来源:互联网 发布:出库软件电脑 编辑:程序博客网 时间:2024/05/16 10:34
转自:http://my.oschina.net/vimfung/blog/64494
我们知道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
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
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
}
0 0
- 让UITableView响应touch事件
- 让UITableView响应touch事件
- 让UITableView响应touch事件
- 让UITableView响应touch事件
- 让UITableView响应touch事件
- 为实现真正的点击空白收起键盘,让UITableView响应touch事件
- 子View不响应touch事件,让父View响应touch事件
- UITableView相应Touch事件
- 如何让UIScrollView里面的UIImageView响应touch事件
- 让Touch 事件只响应一次 的代码
- touch事件的响应过程
- ios下cocos2dx弹出键盘后如何让layer继续响应touch事件
- UIScrollView无法响应touch事件的解决办法
- 如何使UIWebView响应Touch事件
- Touch 与旋转 事件链 响应
- UIScrollView无法响应touch事件的解决办法
- Responding to Touch Events 响应触摸事件
- UILabel ,RTLabel,响应touch事件的方法
- 验证码技术总结(1)-js生成
- IBM openstack 面试
- How to Use Custom TTF Font on iOS
- hadoop2.0 HDFS搭建和HA切换
- GridView绑定,编辑,更新,取消,删除,排序
- 让UITableView响应touch事件
- android图片的裁剪
- hash join优化
- 弹出式菜单PopMenu
- test
- 分类器是如何做检测的
- linux单机环境下利用openmpi实现矩阵乘的并行计算
- Android 中tomcat搭建本地服务器
- ORA-600 -3005和ORA-600-2662/2663