2012-06-29 2 views

ответ

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

Doing HitTest делаешь, кажется, правильный путь

Вы можете реализовать сенсорные события на View, на котором Tableview проживает, как показано ниже.

Также присвойте объекту textField переменной-члену в textFieldDidBeginEditing, чтобы вы могли уйти в отставку с определенного текстового поля, для которого отображается keyborad.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [textFieldObject resignFirstResponder]; 
} 
Смежные вопросы