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 就可以实现点击手势的功能了

 

转载于:https://www.cnblogs.com/deneyZhao/p/5279227.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值