Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

isthisonetaken

macrumors regular
Original poster
Jun 29, 2006
123
0
Hey All,

so I have a table, each section has 2 cells with a text field in each. I create the cells like so:
Code:
- (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;
            tag++;
        }

        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:
Code:
- (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?
 
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).
 
I think you need to set the text view to be the next responder.

I switched the return to:
Code:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.