• UIView加入手势 然后UITableView 加入进这个View 导致UITableView 的单元格点击事件无效


    #import "ViewController.h"
    
    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
       
        UIView * v = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 100)];
        v.backgroundColor = [UIColor redColor];
        [self.view addSubview:v];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewClick:)];
        tap.delegate  = self;
        [v addGestureRecognizer:tap];
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 280, 100)];
        tableView.delegate = self;
        tableView.dataSource = self;
        
        [v addSubview:tableView];
    }
    -(void) viewClick:(UITapGestureRecognizer*) tap
    {
        NSLog(@"点击view");
    }
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 1;
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell * cell = [UITableViewCell new];
        cell.textLabel.text = @"測试";
        return cell;
    }
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"点击单元格");
    }
    
    
    #pragma mark - UIGestureRecognizerDelegate
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
        // 若为UITableViewCellContentView(即点击了tableViewCell),则不截获Touch事件
        if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
            return NO;
        }
        return  YES;
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end

    解决方式是 加入手势的代理 重写- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 函数

    下面是代码



  • 相关阅读:
    设计模式学习——单例模式
    设计模式六大原则
    OIDC in Angular 6
    Angualr6访问API
    Angular6项目搭建
    Springboot 文件上传(带进度条)
    Oracle同义词(synonym)
    hexo从零开始到搭建完整
    Spring Security验证流程剖析及自定义验证方法
    Java设计模式-工厂方法模式(Factory Method)
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/6927077.html
Copyright © 2020-2023  润新知