Question

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!

Was it helpful?

Solution 2

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;
}

OTHER TIPS

In cellForRowAtIndexPath, set the name to something like "myTextfield-<row>". Then you can parse that and know what the prev/next fields are. You are already setting myTextfield.tag to indexPath.row so you won't even have to parse the name. You know the previous and next fields are "myTextfield-<tag-1>" and "myTextfield-<tag+1>", respectively.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top