标签:
实现代码:
生命一个集成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];
}
标签:
原文地址:http://www.cnblogs.com/ramoslin/p/4538284.html