通过触摸UITableView的背景隐藏键盘 [英] Hide keyboard by touching background of UITableView

查看:92
本文介绍了通过触摸UITableView的背景隐藏键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种优化的方法,当UITextFields位于UITableViewCell中时,可以在背景点击时隐藏键盘.我已经编写了一些代码,希望对您有所帮助.

I was searching some optimized method to hide keyboard on background tap when UITextFields are in a UITableViewCell. I have made some code Hope this would help you.

推荐答案

我创建了tableview类别,用于在tableview包含textfield时在后台点击隐藏键盘.

I made a category of tableview for hiding the keyboard on background tap while tableview contains textfield.

我的头文件:

#import <UIKit/UIKit.h>
#import "Utility.h"

@interface UITableView (HitTest)

@end

我的实现文件:

#import "UITableView+HitTest.h"

@implementation UITableView (HitTest)

UITableViewCell *activeCell;

-(UIView*) hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
    NSInteger iterations = 0;
    // check to see if the hit is in this table view
    if ([self pointInside:point withEvent:event])
    {
        UITableViewCell* newCell = nil;

        // hit is in this table view, find out 
        // which cell it is in (if any)
        for (UITableViewCell* aCell in self.visibleCells)
        {
            iterations ++;
            if ([aCell pointInside:[self convertPoint:point toView:aCell] withEvent:event])
            {
                newCell = aCell;
                break;
            }
        }
        if (!newCell)
        { 
            for (UIView *view in activeCell.subviews)
            {
                iterations++;
                if ([view isFirstResponder])
                {
                    [view resignFirstResponder];
                    break;
                }
            }
        }
        else
        {
            activeCell = newCell;
        }
        NSLog(@"total Iterations:%d",iterations);
    }

    // return the super's hitTest result
    return [super hitTest:point withEvent:event];   
}    

@end

这对我来说很好.

这篇关于通过触摸UITableView的背景隐藏键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆