25、[ iOS ] TableView 点击空白隐藏键盘

由于Cell阻挡事件传递的原因,正常情况下点击 tableView 是不会实现隐藏键盘的功能,我们可以给其添加一个Tap点击手势来实现。

    UITapGestureRecognizer *tableViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableViewTouchInSide)];
    tableViewGesture.numberOfTapsRequired = 1;//几个手指点击
    tableViewGesture.cancelsTouchesInView = NO;//是否取消点击处的其他action
    [tableView addGestureRecognizer:tableViewGesture];

实现其方法

// ------tableView 上添加的自定义手势
- (void)tableViewTouchInSide{
    // ------结束编辑,隐藏键盘
    [self.view endEditing:YES];
}

你可能感兴趣的:(25、[ iOS ] TableView 点击空白隐藏键盘)