让UITableView响应touch事件
来源:互联网 发布:软件安全性认识 编辑:程序博客网 时间:2024/06/04 18:51
版权归Aaidong所有〜转载需声名〜 欢迎大家访问:http://blog.csdn.net/aaidong
多说无益,解释/注释代码里都有,直接贴代码:
//// ZJD_TableView.h// 让UITableView响应touch事件//// Created by aidong on 15/5/22.// Copyright (c) 2015年 aidong. All rights reserved.//#import <UIKit/UIKit.h>/** * 我们知道UITableView没有像UIButton那样可以通过addTarget方法来监听touch事件,因此在某些场合,特别是在UITableViewCell中包含UITextField的时候,我们很有可能想通过点击UITableView的其他地方来取消UITextField的焦点。也许有朋友会说,使用UITapGestureRecognizer手势来取消焦点,这样是可以行得通,但是如果TextField中有clearButton或者其自定义的Button的时候,手势就会吸收掉事件了,导致按钮无效。 *//** * 本类使用方法也很简单在原有UITableView的基础上赋予touchDelegate委托即可取到touch事件响应。如下: - (void)loadView { [super loadView]; TouchTableView *tableView = [[TouchTableView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320, 400) style:UITableViewStyleGrouped]; tableView.touchDelegate = self; [self.view addSubview:tableView]; } - (void)tableView:(UITableView *)tableView touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { //touch结束后的处理 } *//** * 重写UITableView的touch相关的方法,然后通过委托的方式提供给外部对象使用。首先定义Delegate: */@protocol TouchTableViewDelegate <NSObject>@optional/** * 重写touch方法时必须把父类实现方法写上,否则UITableViewCell将无法正常工作。所有的改写工作如下所示,新的TableView类具有touch事件响应了。 */- (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相关方法 */@interface ZJD_TableView : UITableView@property (nonatomic,weak) id<TouchTableViewDelegate> touchDelegate;@end
//// ZJD_TableView.m// 让UITableView响应touch事件//// Created by aidong on 15/5/22.// Copyright (c) 2015年 aidong. All rights reserved.//#import "ZJD_TableView.h"@implementation ZJD_TableView- (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(TouchTableViewDelegate)] && [_touchDelegate respondsToSelector:@selector(tableView:touchesMoved:withEvent:)]) { [_touchDelegate tableView:self touchesMoved:touches withEvent:event]; }}@end
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事件的方法
- python 2.x 还是 python 3.x 呢?
- Shiro架构介绍
- 解决 eclispe 保存 java 源码时自动格式问题
- 怎么轻松的把pdf文档转为Excel
- 点击动画弹出指定位置的dialog
- 让UITableView响应touch事件
- Java安全之消息摘要
- spring MVC之用Handler Interceptor拦截请求
- Delphi XE8 及 C++Builder XE8 中文手册即将上市
- Ubuntu 12.04安装rabbitmq
- Linux(Ubuntu)下使用vsftpd搭建ftp服务器
- 黑马程序员----OC常用集合类
- SharePoint 2013 集成嵌入 BI工具 DataZen
- 136A - Presents