Get UITableView to scroll to the selected UITextField and Avoid Being Hidden by Keyboard

У меня есть UITextField в табличном представлении на UIViewController (не a UITableViewController ). Если табличное представление находится на UITableViewController , таблица автоматически переместится к редактируемому текстовому полю , чтобы предотвратить его скрытие с клавиатуры. Но на UIViewController этого не происходит.

Я пытался в течение нескольких дней читать несколько способов, чтобы попытаться это сделать, и я не могу заставить его работать. Ближайший объект, который на самом деле прокручивается, это:

-(void) textFieldDidBeginEditing:(UITextField *)textField {

// SUPPOSEDLY Scroll to the current text field

CGRect textFieldRect = [textField frame];
[self.wordsTableView scrollRectToVisible:textFieldRect animated:YES];

}

Однако это только прокручивает таблицу до самой верхней строки. То, что кажется легкой задачей, превратилось в пару дней разочарования.

Я использую следующее для создания ячеек tableView:

- (UITableViewCell *)tableView:(UITableView *)aTableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *identifier = [NSString stringWithFormat: @"%d:%d", [indexPath indexAtPosition: 0], [indexPath indexAtPosition:1]];

UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] 
        initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:identifier] autorelease];

        cell.accessoryType = UITableViewCellAccessoryNone;

        UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(180, 10, 130, 25)];

        theTextField.adjustsFontSizeToFitWidth = YES;
        theTextField.textColor = [UIColor redColor];
        theTextField.text = [textFieldArray objectAtIndex:indexPath.row];
        theTextField.keyboardType = UIKeyboardTypeDefault;
        theTextField.returnKeyType = UIReturnKeyDone;
        theTextField.font = [UIFont boldSystemFontOfSize:14];
        theTextField.backgroundColor = [UIColor whiteColor];
        theTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        theTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        theTextField.clearsOnBeginEditing = NO;
        theTextField.textAlignment = UITextAlignmentLeft;

        //theTextField.tag = 0;
        theTextField.tag=indexPath.row;

        theTextField.delegate = self;

        theTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [theTextField setEnabled: YES];

        [cell addSubview:theTextField];

        [theTextField release];


}

return cell;
}

Я подозреваю, что смогу заставить tableView прокручиваться должным образом, если смогу каким-то образом передать ] indexPath.row в методе textFieldDidBeginEditing ?

Любая помощь приветствуется.

46
задан Midas 20 February 2014 в 09:30
поделиться