tableview 繼承自UIScrollView ,在手勢上面會有很多沖突,比如在cell 上面添加了一個textfield 的時候,要求實現 點擊textfield 以外的地方實現 [tetxfield resignFirstResponder]; 這樣的話 點擊手勢就會失效。
解決辦法是實現 category重寫tableview 的方法:
.h文件:
#import <UIKit/UIKit.h>
/**
* 重寫tableview 的touch 事件,使tableview 能夠響應touch 事件
*/
@protocol TableViewTouchDelegate <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
static NSString *TableViewTouchesBeginNotifaction;
static NSString *TableViewTouchesCancelledNotifaction;
static NSString *TableViewTouchesEndedNotifaction;
static NSString *TableViewTouchesMovedNotifaction;
@interface TouchTableView : UITableView
@property (nonatomic,assign) id<TableViewTouchDelegate> touchDelegate;
@end
.m文件:
#import "TouchTableView.h"
@implementation TouchTableView
@synthesize touchDelegate = _touchDelegate;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
if ([_touchDelegate conformsToProtocol:@protocol(TableViewTouchDelegate)] &&
[_touchDelegate respondsToSelector:@selector(tableView:touchesBegan:withEvent:)])
{
[_touchDelegate tableView:self touchesBegan:touches withEvent:event];
}
[[NSNotificationCenter defaultCenter]postNotificationName:TableViewTouchesBeginNotifaction object:nil];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
if ([_touchDelegate conformsToProtocol:@protocol(TableViewTouchDelegate)] &&
[_touchDelegate respondsToSelector:@selector(tableView:touchesCancelled:withEvent:)])
{
[_touchDelegate tableView:self touchesCancelled:touches withEvent:event];
}
[[NSNotificationCenter defaultCenter]postNotificationName:TableViewTouchesCancelledNotifaction object:nil];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
if ([_touchDelegate conformsToProtocol:@protocol(TableViewTouchDelegate)] &&
[_touchDelegate respondsToSelector:@selector(tableView:touchesEnded:withEvent:)])
{
[_touchDelegate tableView:self touchesEnded:touches withEvent:event];
}
[[NSNotificationCenter defaultCenter]postNotificationName:TableViewTouchesEndedNotifaction object:nil];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
if ([_touchDelegate conformsToProtocol:@protocol(TableViewTouchDelegate)] &&
[_touchDelegate respondsToSelector:@selector(tableView:touchesMoved:withEvent:)])
{
[_touchDelegate tableView:self touchesMoved:touches withEvent:event];
}
[[NSNotificationCenter defaultCenter]postNotificationName:TableViewTouchesMovedNotifaction object:nil];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
然后 新建 一個繼承自tableview 的view 就可以實現點擊手勢的功能了