0
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"searchCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    searchField = (UITextField *)[cell viewWithTag:10000];

    [searchField resignFirstResponder];
}

Okay, I have a cell with a identifier name of "searchCell". This cell contains one single UITextField with tag 10000 that pops up a keyboard on the bottom of the screen. When a user touches another cell, the keyboard must be hidden so that the user can have larger space to scroll up and down.

However, when a keyboard has popped up and a user touches(select) a cell, the code above is called but not working... :( It seems like the assigned UITableViewCell is not the one that the user is currently using. What am I doing wrong here?

Raccoon
  • 1,367
  • 4
  • 22
  • 45
  • Adding 'forIndexPath' didn't help at all btw. – Raccoon Jan 23 '13 at 19:40
  • Do you have delegates set in your nib and in your .h file ? Also do this => UITextField *txtFieldObj = (UITextField *)[cell viewWithTag:10000]; Also do this in didselectRowAtIndexpath, add an if(yourIndexpath.row==0) then only resign keyboard or if(yourindexpath!=0) then resign else do something else.. [txtFieldObj resignFirstResponder]; Also NSLog(@"txtField %@",textFieldObj); // check if it is non empty.. – Reno Jones Jan 23 '13 at 19:41
  • did you set delegate of your textfield? if not try `textfield searchfield.delegate = self;` and call `[searchField resignFirstResponder];` in `-(BOOL)textFieldShouldReturn:(UITextField *)textField` method – SpaceDust__ Jan 23 '13 at 19:46
  • Oh, I also was supposed to use didSelectRowAtIndexPath.. not 'didDeselect...' :) – Raccoon Jan 23 '13 at 20:12

1 Answers1

2
  1. Make your class a delegate of UITextField
  2. Go to the Storyboard file, click on the text field and go to connections inspector
  3. Under outlets, connect the delegate to the View Controller
  4. Run it in simulator. It will work
Ashish Agarwal
  • 14,555
  • 31
  • 86
  • 125