objective-cuitextfieldtableviewbecomefirstresponder

Moving to next textField inside an UITableView


I have a tableView full of sub-classed textFields. Below is how i created them:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = kOddCellIdentifier;

    CustomCell01 *oddCell = (CustomCell01 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    oddCell.myTextfield.delegate=self;
    oddCell.myTextfield.tag = indexPath.row;
    oddCell.myTextfield.text=[[self.Page01dataArray objectAtIndex:indexPath.row]objectForKey:@"value"];
    bool editable = [[[self.Page01dataArray objectAtIndex:indexPath.row] objectForKey:@"editable"] boolValue];
    if (editable==true) {
        oddCell.myTextfield.textColor=[UIColor redColor];
    } else {
        oddCell.myTextfield.textColor=[UIColor blackColor];
        [oddCell.myTextfield setBorderStyle:UITextBorderStyleNone];
        oddCell.myTextfield.enabled=NO;
    }
    [oddCell.myTextfield addTarget:self action:@selector(updateField:) forControlEvents:UIControlEventEditingDidEnd];

    return oddCell;
}

I do not know how to move to the next textfield when the user taps on the "Next" button. Normally i should make the next textfield the first responder however since all my textfields have the same name "myTextfield", i do not know how to do it. Please help!


Solution

  • I finally managed to find out a solution by creating an array in cellForRowAtIndexPath

    [nextTFarray addObject:oddCell.myTextfield]; 
    

    and then:

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        int i=[nextTFarray count];
        int x=textField.tag+1;
        UITextField *nextTextField = (UITextField *)[nextTFarray objectAtIndex:x];
        if (x<i-1) {
            [nextTextField becomeFirstResponder];
        } else {
            [textField resignFirstResponder];
        }
        return YES;
    }