UITouch和TableView如何响应点击事件

实现代码:

生命一个集成UITableView的类

#import <UIKit/UIKit.h>

@protocol CHTouchTableViewDelegate <NSObject>

@optional

- (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

@interface CHTouchTableView : UITableView

@property (nonatomic,weak) id <CHTouchTableViewDelegate> touchDelegate;

@end

实现:

#import "CHTouchTableView.h"

@implementation CHTouchTableView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

[super touchesBegan:touches withEvent:event];

if ([_touchDelegate conformsToProtocol:@protocol(CHTouchTableViewDelegate)] &&

[_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(CHTouchTableViewDelegate)] &&

[_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(CHTouchTableViewDelegate)] &&

[_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(CHTouchTableViewDelegate)] &&

[_touchDelegate respondsToSelector:@selector(tableView:touchesMoved:withEvent:)])

{

[_touchDelegate tableView:self touchesMoved:touches withEvent:event];

}

}

@end

测试使用代码:

#import "ViewController.h"

#import "CHTouchTableView.h"

@interface ViewController () <UITableViewDataSource,UITableViewDelegate,CHTouchTableViewDelegate>

@property (nonatomic,weak) CHTouchTableView * tableView;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];

[self.view addSubview:textFiled];

textFiled.placeholder = @"pls input the name";

[textFiled addTarget:self action:@selector(textDidChanged:) forControlEvents:UIControlEventEditingChanged];

CHTouchTableView * tableView  = [[CHTouchTableView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height-100) style:UITableViewStylePlain];

[self.view addSubview:tableView];

self.tableView = tableView;

self.tableView.delegate = self;

self.tableView.dataSource = self;

self.tableView.touchDelegate = self;

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CellID"];

tableView.tableFooterView = [[UIView alloc] init];

}

- (void) textDidChanged:(UITextField *) sender

{

NSLog(@"the text DidChanged: %@",sender.text);

}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return 30;

}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CellID"];

cell.textLabel.text = [NSString stringWithFormat:@"Cell number = %@",indexPath];

return cell;

}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

NSLog(@"the cell did selected at indexpath = %@",indexPath);

}

- (void) tableView:(UITableView *)tableView touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

[self.view endEditing:YES];

}

时间: 2024-07-31 17:59:49

UITouch和TableView如何响应点击事件的相关文章

HttpWebRequest 模拟登录响应点击事件(开源自己用的HttpHelper类)

平时也经常采集网站数据,也做模拟登录,但一般都是html控件POST到页面登录:还没有遇到用户服务器控件button按钮点击事件登录的,今天像往常一样POST传递参数,但怎么都能登录不了:最后发现还有两个参数需要传,__EVENTVALIDATION和__VIEWSTATE 在传的过程中需要对参数值进行URL编码 System.Web.HttpUtility.UrlEncode(value) 模拟登录代码:在本地写的一个测试的网站来模拟登录,原理都一样: Request request = ne

设置TextView下划线并响应点击事件(SpannableString)

写Demo程序的时候能表带自定义的数据结构对象吗? --低级程序猿 前情提要:网上介绍TextView+SpannableString的文章真心太长,真心看不懂. ====原文===== 下面是一个20行的完整Demo代码:基本原理是使用一个SpannableString并设置其ClickableSpan来响应点击事件. TextView useInfo = (TextView) findViewById(R.id.info); String url_0_text = "用户协议及隐私条款&qu

[android篇]textview中片段响应点击事件(SpannableString)

项目需求 点击textView中的一小段文字,弹一个dialog框 失败解决方案 刚开始是用了两个textView水平布局,可想而知,当第一个textView快占满一行,还未换行时,第二个textView很可能出现换行排版问题 用spannableString的问题 小段文字有下划线 点击textView中的小段文字时,系统会当做url处理,给点击部分的text加一个蓝色的背景 解决方案 public class TouchableSpan extends ClickableSpan { pri

iOS开发随笔 31:UIButton无法响应点击事件

问题描述 因为项目需要,需要UITableView上添加固定的筛选表头,一直固定,不能随UITableView滚动.所以直接将表头与UITableView分离,将它添加到控制器的UIView上,即添加到UITableView的父视图上,与UITableView同级.然后表头上添加三个UIButton,效果达到预期,但问题随即出现.筛选表头上的UIButton无法响应点击事件,刚开始以为造成的原因是手误把UIButton的父视图或者UIButton属性userInteractionEnabled被

popupwindow放置listview,并且使listview可以响应点击事件

最近在模仿微信的选择头像的功能,就是一个Popwindow里面是一个ListView,ListView是所有相册,用户点击里面的相册,那么那个相片展示的页面就会显示你选择的相册的相片 .刚开始发现里面的ListView不能获取点击事件,插入如下代码解决 popWinPlaylist.setFocusable(true); popWinPlaylist.update(); 随后发现popwindown以外的地方不能响应点击事件,其实你可以通过pw(PopupWindow对象).getContext

无法响应点击事件listview.OnItemClickListener 11_13

无法响应点击事件OnItemClickListener android:descendantFocusability="blocksDescendants"即可.这样Item Layout就屏蔽了所有子控件获取Focus的权限, beforeDescendants:viewgroup会优先其子类控件而获取到焦点 afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点 blocksDescendants:viewgroup会覆盖子类控件而直接获得焦

IOS UIImageView直接响应点击事件的解决方法

UIImageView的手势处理可以响应点击事件. 1.  self.allScreenImg.userInteractionEnabled = YES;//UIImageView默认不响应手势,需要开启userInteractionEnabled //处理单击事件 UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAllScr

iOS 为自定义tableView添加button点击事件后获取其序号

在自定义tableView中,为cell添加button点击事件后,如何获取其对应的序号? 1.创建tableView: 先创建一个成员变量: @interface MyCameraViewController ()<UITableViewDelegate,UITableViewDataSource> { UITableView *_tableView; }@end 在viewDidLoad中初始化 _tableView = [[UITableView alloc] initWithFrame

button不在父试图中,但是需要响应点击事件

当button不在父试图的范围内时,是无法响应点击事件的.项目中涉及了这个部分,之后写了个小demo(点击按钮,向上弹出两个button,并且都能响应点击事件),如下: 自定义view:CUMoreView ////  CUMoreView.h// #import <UIKit/UIKit.h> typedef void(^btnClickBlock)(UIButton *btn); @interface CUMoreView : UIView @property (nonatomic, co