通过触摸UITableView的背景隐藏键盘

时间:2012-06-29 10:44:14

标签: objective-c ios uitableview keyboard uitextfield

UITextFields位于UITableViewCell时,我正在搜索一些优化方法来隐藏背景上的键盘。我已经做了一些代码希望这会对你有帮助。

2 个答案:

答案 0 :(得分:1)

我在tableview包含tableview时隐藏键盘的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

我的实施档案:

{{1}}

这对我来说很好。

答案 1 :(得分:1)

hittest doest似乎是正确的方法

您可以在tableView所在的View上实现触摸事件,如下所示。

还将textField对象分配给textFieldDidBeginEditing中的成员变量,这样您就可以重新签名显示keyborad的特定文本字段。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [textFieldObject resignFirstResponder];
}
相关问题