UITextFields
我正在寻找一些优化的方法来隐藏后台点击时的键盘UITableViewCell
。我已经做了一些代码希望这会对你有所帮助。
问问题
1910 次
2 回答
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
这对我来说很好。
于 2012-06-29T10:45:40.103 回答
1
做hittest似乎不是正确的方法
您可以在 tableView 所在的 View 上实现触摸事件,如下所示。
还将textField 对象分配给 中的成员变量textFieldDidBeginEditing
,这样您就可以放弃显示键盘的特定文本字段。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[textFieldObject resignFirstResponder];
}
于 2012-07-06T10:12:30.740 回答