11

I have a textField that is set to change the tableView's dataSource with each letter that's entered (and call reloadData).

But for some reason, every time a letter is entered, the keyboard is dismissed.

Anyone know why?

Will Larche
  • 3,119
  • 7
  • 26
  • 34
  • 1
    Post some code of the methods for beginning the editing of your text field and any other relevant methods, we can't help you if we can't see what the problem is. – 8vius Jul 07 '12 at 05:41
  • 1
    possible duplicate of [UITableView reloadData resigns first responder](http://stackoverflow.com/questions/6409370/uitableview-reloaddata-resigns-first-responder) – CodaFi Jul 07 '12 at 05:43
  • @CodaFi you nailed it. The textField was in a custom cell at the top of the table so when I reloadData it reloads that cell too thereby resigning anything inside it. THANK YOU!!! – Will Larche Jul 07 '12 at 16:31
  • I will put it up as the answer for you to accept. – CodaFi Jul 07 '12 at 16:31
  • Try the approach explained here: http://stackoverflow.com/a/16462473/468868 – Carlos Ricardo May 09 '13 at 13:03

2 Answers2

14

Your text field is resigning because reloaded cells are sent a -resignFirstResponder message due to the fact that their survival is not guaranteed after a reload. See this related question for more.

Community
  • 1
  • 1
CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • A nice workaround is adding a hidden textfield to the view, and switching focus over to that textfield when reloading the table. After the table is reloaded, you can switch the focus back to your original textfield, to keep that keyboard open. [self.fakeTextField becomeFirstResponder]; [self.tableView reloadData]; [self.textField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:.01]; – hobosf Jun 12 '15 at 19:27
-6

Use this method textFieldShouldReturn: and add UITextFieldDelegate delegate in yourClass.h file. set delegate to yourTextfield and write following code in viewDidLoad method.

yourTextfield.delegate = self;

and also implement the textFieldShouldReturn: as following as

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {

        [theTextField resignFirstResponder];

   return YES; 
}

I think it will be helpful to you.

Prasad G
  • 6,702
  • 7
  • 42
  • 65