键盘隐藏UITableView问题

时间:2013-08-01 07:16:13

标签: objective-c

我有一个视图控制器,我有一个表视图,在表视图中每行都有文本字段,当我点击文本字段键盘出现并隐藏表视图,然后看不到编辑,怎么能我解决了这个问题?我把观察者修复了视图控制器的对齐方式但是这里没有用的是代码..

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];
 return YES;
}

-(void)keyboardWillShow {
// Animate the current view out of the way
[UIView animateWithDuration:0.3f animations:^ {
    self.viewFrame =  CGRectMake(0, -160, 320, 480);
}];
}

2 个答案:

答案 0 :(得分:2)

you have to write below code, it will hide and show keyboard.   

====>.h file: declare one UITextField as below:

UITextField *actifText;

====>.m file:

-(void)viewDidAppear:(BOOL)animated
{
  // Register notification when the keyboard will be show
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

// Register notification when the keyboard will be hide
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}

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

-(void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void) keyboardWillShow:(NSNotification *)note
{
// Get the keyboard size
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];

// Detect orientation
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect frame = self.tblName.frame;

// Start animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];

// Reduce size of the Table view 
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
    frame.size.height -= keyboardBounds.size.height;
else 
    frame.size.height -= keyboardBounds.size.width;

// Apply new size of table view
self.tblName.frame = frame;

// Scroll the table view to see the TextField just above the keyboard
if (self.actifText)
{
    CGRect textFieldRect = [self.tblName convertRect:self.actifText.bounds fromView:self.actifText];
    [self.tblName scrollRectToVisible:textFieldRect animated:NO];
}

[UIView commitAnimations];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.actifText = nil;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
   self.actifText = textField;
}

-(void) keyboardWillHide:(NSNotification *)note
{
// Get the keyboard size
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];

// Detect orientation
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect frame = self.tblName.frame;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];

// Reduce size of the Table view 
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
    frame.size.height += keyboardBounds.size.height;
else 
    frame.size.height += keyboardBounds.size.width;

// Apply new size of table view
self.tblName.frame = frame;

[UIView commitAnimations];
}

答案 1 :(得分:0)

当您点击单元格内的某些文本字段时,您必须知道当前单元格的索引路径,因此当显示键盘时,您可以将其聚焦。

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
    UITableViewCell *cell = (UITableViewCell *)[textField superview].superview;
    NSIndexPath *idxPath = [table indexPathForCell:cell];
    selectedIndex = idxPath.row;//instance variable

    return YES;
}

-(void)keyboardWillShow {

[table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:selectedIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

还要在ViewDidLoad添加以下行,请记住键盘何时出现。如果您使用UIKeyboardWillShowNotification,则会先调用keyboardWillShow,然后调用textFieldShouldBeginEditing:,这是错误的。

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

并设置

textfield.delegate = self;
创建文本字段时在cellForRowAtIndexPath

。它将调用所需的方法。