2013-08-01 94 views
4

我有,我有一個表視圖視圖控制器,查看錶的每一行具有文本字段,當我點擊文本字段鍵盤上顯示和隱藏表視圖,然後看不到編輯,我該如何解決這個問題?我把的觀察者認爲解決視圖控制器的定位,但它並沒有在這裏工作是代碼..鍵盤隱藏UITableView的問題

- (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); 
}]; 
} 
+0

你需要設置contentOffset –

+0

嘿檢查我的答案。 – NightFury

回答

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]; 
} 
+0

工作,但是當我敲擊這是對錶視圖的頂部,然後當我在文本視圖.. – Anu

+0

代碼爲書面挖掘表視圖向上的每一次移動文本視圖,因爲如果數據比較多,在那個時候它會我自動上移。如果你不想這樣做然後刪除//滾動表視圖,以查看鍵盤上方的TextField if(self.actifText) CGRect textFieldRect = [self.tblName convertRect:self.actifText.bounds fromView:self.actifText ]。 [self.tblName scrollRectToVisible:textFieldRect animated:NO]; } from - (void)keyboardWillShow:(NSNotification *)note方法 – Mital

0

當您在細胞內一些文本框挖掘,你必須知道當前單元格的indexpath,所以當顯示鍵盤,你可以關注它。

-(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在創建文本框。它會調用所需的方法。