0
votes

I have a UITableView of custom UITableViewCells each with two UITextFields in them. When I select a UITextField in the opening view i.e. without scrolling select a UITextField in the opening view. It becomesFirstResponder and I can enter text, as I hit enter all UITextFields in the opening view can becomeFirstResponder but when I get to the end of the UITableViewCells of that opening view and try to load the next UITableViewCell that is currently out of view and set its UITextField to becomeFirstResponder it doesn't work.

I have set up a if statment inside textFieldShouldReturn and it confirms that the textField is returning NO to becomeFirstResponder.

I would like to know how to fix this; I have looked for solutions but have yet to find anything that fits my situation. Below are the UITextFieldDelegates that I am using as I think in here I am supposed to do something like call or load the next UITableViewCell, but I do not know how.

#pragma mark - Textfield Delegates
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];

    if ([textField isEqual:cell.widthTexField]) {
        nWidth = [[sortedItemsArray objectAtIndex:textField.tag] valueForKey:@"w_MM"];
    } else if ([textField isEqual:cell.heightTextField]) {
        nHeight = [[sortedItemsArray objectAtIndex:textField.tag] valueForKey:@"h_MM"];
    }
    return YES;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField*)textfield {

        int height = self.finishingTableView.frame.size.height;
        NSLog(@"%i", height);
         self.finishingTableView.frame= CGRectMake(self.finishingTableView.frame.origin.x, self.finishingTableView.frame.origin.y, self.finishingTableView.frame.size.width, 307);

    // select correct row
    if (textfield.tag > 999999) {
        int adjustTag = textfield.tag-1000000; // remove a million so that you have the text fields correct position in the table. (this is only for height textfields)
        NSIndexPath *indexPath =[NSIndexPath indexPathForRow:adjustTag inSection:0];
        [finishingTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        [self tableView:finishingTableView didSelectRowAtIndexPath:indexPath];
        [self.finishingTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
        return YES;
    } else {
        NSIndexPath *indexPath =[NSIndexPath indexPathForRow:textfield.tag inSection:0];
        [finishingTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        [self tableView:finishingTableView didSelectRowAtIndexPath:indexPath];
        [self.finishingTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
        return YES;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"%i", textField.tag+1);
    [[self.view viewWithTag:textField.tag+1] becomeFirstResponder];

    BOOL success = [[self.view viewWithTag:textField.tag+1] becomeFirstResponder];
    if (success) {
        NSLog(@"HypnosisView became the first responder");
    } else {
        NSLog(@"Could not become first responder");
    }

    // this means there has been a change in the UItextfield


    NSLog(@"%@", selectedItemDictionary);
    if (textField.tag < 999999) {
        tempFinishingObjectDictionary = [selectedItemDictionary mutableCopy];
        if (![textField.text isEqualToString:[selectedItemDictionary objectForKey:@"w_MM"]]) {
            tempUpdatedRow = @"T";
            // remove kevalues
            [tempFinishingObjectDictionary removeObjectForKey:@"updatedRow"];
            [tempFinishingObjectDictionary removeObjectForKey:@"new_w_MM"];
            // update keyvalues
            [tempFinishingObjectDictionary setValue:tempUpdatedRow forKey:@"updatedRow"];
            [tempFinishingObjectDictionary setValue:textField.text forKey:@"new_w_MM"];
        }
    } else {
        if (![textField.text isEqualToString:[selectedItemDictionary objectForKey:@"h_MM"]]) {
            tempUpdatedRow = @"T";
            // remove kevalues
            [tempFinishingObjectDictionary removeObjectForKey:@"updatedRow"];
            [tempFinishingObjectDictionary removeObjectForKey:@"new_h_MM"];
            // update keyvalues
            [tempFinishingObjectDictionary setValue:tempUpdatedRow forKey:@"updatedRow"];
            [tempFinishingObjectDictionary setValue:textField.text forKey:@"new_h_MM"];
        }
    }
    NSLog(@"%@", tempFinishingObjectDictionary);
    [coreDataController editSelectedFinishing:htmlProjID UpdatedNSD:tempFinishingObjectDictionary SelectedRow:selectedItemIndexPathRow];
    [SVProgressHUD dismiss];


    NSLog(@"%@", selectedItemDictionary);

    return YES;
}
1

1 Answers

0
votes

In your textFieldShouldReturn when you go to the next cell you should check if two cells out is off the screen (and therefore not existent). If it is iff the screen you should scroll the table by one cell

[tableview setContentOffset:CGPointMake(tableview.contentOffset.x,tableview.contentOffset.y + cellHeight) animated:YES];

You will have to adjust this scrolling to your liking as I don't know how your application flows.