2014-02-20 52 views
1

我正在開發一個iPhone應用程序。我在我的ViewController上有一個簡單的UITableView。在UITableView中,我在單元格中添加UITextField。我的問題是當我點擊特定單元中的UITextField時,鍵盤隱藏了整個UITableView。我已經搜索過它,但無法成功找到解決方案。我加入UITextField這樣的:鍵盤隱藏StoryBoard中的UITableView

UITextField* textField = [[UITextField alloc]initWithFrame:CGRectMake(250, 15, 60, 30)]; 
textField.delegate = self; 
textField.borderStyle = UITextBorderStyleRoundedRect; 
textField.autocorrectionType = UITextAutocorrectionTypeNo; 
textField.backgroundColor = [UIColor clearColor]; 
textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; 
textField.tag =TEXT_VIEW_TAG; 
[cell.contentView addSubview:textField]; 

如果有人知道它的解決方案,那麼請讓我知道。

+0

請提供您的表視圖數據源和委託方法的代碼,以更深入地瞭解您的問題。 –

回答

2

添加UITableViewController實際上可以自己處理。但是,如果您確實需要使用UITableView,但仍然希望鍵盤不要隱藏您的UITableView,則在鍵盤打開時您需要向上移動UITableView。爲此,您可以更改UITextFieldBeginEditing方法中的y位置,也可以使用UIKeyboard通知。您將需要添加的代碼下面的線在你viewDidLoad方法:

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(myKeyboardWillHideHandler:) 
               name:UIKeyboardWillHideNotification 
               object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(myKeyboardWillShowHandler:) 
               name:UIKeyboardWillShowNotification 
               object:nil]; 

然後,您可以添加這些方法,如:在你的TextBeginEditing

- (void) myKeyboardWillHideHandler:(NSNotification *)notification { 

      [UIView animateWithDuration:0.2 animations:^{ 
       YOUR_TBL_VIEW.frame = CGRectMake(YOUR_TBL_VIEW.frame.origin.x, YOUR_TBL_VIEW.frame.origin.y + 80, YOUR_TBL_VIEW.frame.size.width, YOUR_TBL_VIEW.frame.size.height); 
      }]; 
     } 

    - (void) myKeyboardWillShowHandler:(NSNotification *)notification { 
      [UIView animateWithDuration:0.2 animations:^{ 
       YOUR_TBL_VIEW.frame = CGRectMake(YOUR_TBL_VIEW.frame.origin.x, YOUR_TBL_VIEW.frame.origin.y - 80, YOUR_TBL_VIEW.frame.size.width, YOUR_TBL_VIEW.frame.size.height); 
      }]; 


} 
+0

非常感謝你...非常感謝你。最後它解決了我的問題:) –

0

你可以拿UITableViewController類而不是UIViewController這將解決你自己的問題。

+0

感謝您的回覆。讓我嘗試。 –

1

嘗試加入這一行代碼在textFieldDidBeginEditing方法

[self.tableView setContentOffset:CGPointMake(0,textField.center.y-170) animated:YES]; 

這裏170是適合我的價值,ü可以嘗試其他。希望它可以幫助。

0

一個一行代碼

[self.tableView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

它會讓你排在最前面有那麼多的位置在那裏你可以相應地改變。