2013-07-16 107 views
0

我想,当用户点击的TableViewCell.m为UITextFieldIOS检测触摸关闭键盘?

外弄清楚如何消除键盘和触发方法:

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [self.delegate cellDidBeginEditing:self]; 
} 

在ViewController.m:

-(void)cellDidBeginEditing:(TableViewCell *)editingCell 
{ 
_editingOffset = _tableView.scrollView.contentOffset.y - editingCell.frame.origin.y; 
for (TableViewCell *cell in [_tableView visibleCells]) { 
    [UIView animateWithDuration:0.3 
        animations:^{ 
         cell.frame = CGRectOffset(cell.frame, 0, _editingOffset); 
         if (cell != editingCell) { 
          cell.alpha = 0.25; 

         } 
        }]; 
    } 
} 

cellDidBeginEditing:方法将单元格替换为顶部并使其他单元格变成灰色。

我有另一种方法,cellDidEndEditing:它做了与此相反的,并且代码是不是真的需要。

截至目前,选择,例如,“小区2”编辑“小区1”时,只是触发cellDidBeginEditing为“小区2”

我想键盘解雇和cellDidEndEditing触发,当我点击“小区1”之外

+0

的可能重复(http://stackoverflow.com/questions/5306240/iphone-dismiss-keyboard-when-touching-outside-of-textfield)[iphone,触摸文本域之外时关闭键盘] –

回答

1

尝试实现和调用这些函数:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSLog(@"touchesBegan:withEvent"); 
    [self.view endEditing:YES]; 
    [super touchesBegan:touches withEvent:event]; 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [self.firstTextField resignFirstResponder]; 
    [self.secondTextField resignFirstResponder]; 
    return YES; 
} 

这应该让这个当你触摸任何地方两个文本框之外,键盘自动解散。

+0

我把在ViewController.m中的第一个方法,它没有做任何事情。并且我没有“firstTextField”和“secondTextField”作为TableViewCell下的对象。 我在想UITapGesture,但可以使用指导。 – Brian

+0

@Brian发布了一些更多的代码,显示你的.m文件夹中的文本字段的位置。 'resignFirstResponder'是什么应该关闭键盘。如果你想使用触摸手势,你可以使用'[self.view endEditing:YES];'当你触摸。 – CaptJak

+0

' - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return NO; }' – Brian