UITextFields within UITableView not using becomeFirstResponder

Discussion in 'iOS Programming' started by isthisonetaken, Mar 5, 2012.

  1. macrumors regular

    Jun 29, 2006
    Hey All,

    so I have a table, each section has 2 cells with a text field in each. I create the cells like so:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        static NSString *CellIdentifier = @"Fuel Surcharge Cell";
        FuelSurchargeCell *cell = [self.fuelSurchargeTableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[FuelSurchargeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        // configure cell
        NSMutableDictionary *currentDictionary = [self.fuelSurchargeArray objectAtIndex:indexPath.section];
        NSMutableArray *descriptions = [currentDictionary objectForKey:@"descriptions"];
        NSMutableArray *surcharges = [currentDictionary objectForKey:@"surcharges"];
        cell.descriptionLabel.text = [descriptions objectAtIndex:indexPath.row];
        if ([surcharges objectAtIndex:indexPath.row]) {
            cell.inputField.text = [surcharges objectAtIndex:indexPath.row];
            NSInteger tag = indexPath.section;
            if (indexPath.row == 0) {
                tag *= 2;
            } else {
                tag *= 2;
            cell.inputField.tag = tag;
            cell.inputField.delegate = self;
            cell.inputField.returnKeyType = UIReturnKeyNext;
            [self.fscTextFields addObject:cell.inputField];
        return cell;
    I would like it so when the user is done entering values, they can hit next and have the focus shift to the next cell. From searching Google, I came up with this:
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
        FuelSurchargeCell *currentCell = (FuelSurchargeCell *)textField.superview.superview;
        NSIndexPath *currentIndexPath = [self.fuelSurchargeTableView indexPathForCell:currentCell];
        NSIndexPath *nextIndexPath;
        if (currentIndexPath.row == 0) {
            nextIndexPath = [NSIndexPath indexPathForRow:1 inSection:currentIndexPath.section];
        } else {
            nextIndexPath = [NSIndexPath indexPathForRow:0 inSection:(currentIndexPath.section + 1)];
        [self.fuelSurchargeTableView scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 
        return [[self.fuelSurchargeTableView cellForRowAtIndexPath:nextIndexPath] becomeFirstResponder];
    However, it never works! It always returns no. I've got the array of text fields, and I tried using that to set the next object in the array (based on the tags) to becomeFirstResponder. Still no dice.

    Any ideas?
  2. macrumors regular

    Oct 18, 2007
    I just went through some stuff with text fields in table view cells. A handful of thoughts:

    a) What happens when you tell a UITableViewCell to becomeFirstResponder? Maybe you meant the UITextField subview in that cell?

    b) Stop in the debugger on the line that tries to change responder and look at the local variables and see how they're set.

    c) You can select the CELL or you can select the TEXT FIELD. If you select the UITextField, then the tableview has no currently selected row. Is it possible that the currently selected row is nil and that's causing some issues (even though I see you're going up the view hierarchy).
  3. macrumors 68030


    Sep 2, 2008
    I think you need to set the text view to be the next responder.
  4. thread starter macrumors regular

    Jun 29, 2006
    I switched the return to:
    return [[(FuelSurchargeCell *)[self.fuelSurchargeTableView cellForRowAtIndexPath:nextIndexPath] inputField]  becomeFirstResponder];
    But still, the focus stays in the current textfield instead of moving to the next. I'll try what RonC said and report back later.

Share This Page