tableView 上面 点击手势失效的解决办法
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 就可以实现点击手势的功能了