2010-06-08 4 views
0

J'ai un UITableView, que je contrôle d'une coutume UIViewController. Lorsque l'utilisateur clique sur le bouton 'ajouter', j'ajoute une ligne au UITableView avec un champ de texte, et en fait le premier répondeur. Le problème est que lorsque le bas de la table est hors de vue (ou caché par le clavier), le UITableView ne défile pas pour afficher le champ de texte.Comment puis-je obtenir un UITableView personnalisé pour faire défiler automatiquement vers un champ de texte sélectionné?

UITableViewController le fait automatiquement, mais mon View Controller ne peut pas être une sous-classe de UITableViewController.

+0

double possible de [Faire défiler UITextField ci-dessus du clavier dans un UITableViewCell sur un UIViewController régulier] (http: // stackoverflow .com/questions/15036519/scroll-uitextfield-above-keyboard-in-a-uitableviewcell-on-a-regular-uiviewcontro) –

Répondre

9

J'ai corrigé ceci en twittant contentInset sur le UITableView lorsque le clavier apparaît ou disparaît.

- (void)registerForKeyboardNotifications { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardDidShowNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasHidden:) 
               name:UIKeyboardDidHideNotification object:nil]; 
} 

- (void)keyboardWasShown:(NSNotification *)aNotification { 
    CGRect keyboardBounds; 
    [[aNotification.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds]; 
    keyboardHeight = keyboardBounds.size.height; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0); 
    [UIView commitAnimations]; 
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[items count] inSection:0] 
        atScrollPosition:UITableViewScrollPositionMiddle 
          animated:YES]; 
} 

- (void)keyboardWasHidden:(NSNotification *)aNotification { 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    tableView.contentInset = UIEdgeInsetsZero; 
    [UIView commitAnimations]; 
} 

appel registerForKeyboardNotifications lorsque vous chargez le UITableView, et tout le reste devrait fonctionner.

+0

[tableView scrollToRowAtIndexPath: [NSIndexPath indexPathForRow: [nombre d'éléments] inSection: 0] atScrollPosition: UITableViewScrollPositionMiddle animé: YES]; Que veulent dire les "objets" ici? – iOSDev

+0

C'est un NSArray. – nornagon

+0

Dans le cas où quelqu'un serait intéressé par la façon dont UIKit gère les animations dans ce cas (j'ai eu le même problème), lisez ici: http://www.cocoabuilder.com/archive/cocoa/316466-resizing-uitableview-will-keeping-content -scrolled-to-bottom.html –

0

travail pour iOS5

UITextField *activeField; 

// Called when the UIKeyboardDidShowNotification is sent. 

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    activeField = textField; 
} 
- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    activeField = nil; 
} 

// Called when the UIKeyboardWillHideNotification is sent 

- (void)keyboardWasShown:(NSNotification *)aNotification { 


    id view = [activeField superview]; 
    while (view && ![view isKindOfClass:[UITableViewCell class]]) 
    { 
     view = [view superview]; 
    } 
    UITableViewCell *cell = view; 
    NSIndexPath *indexPath = [tableview indexPathForCell:cell]; 
    CGRect keyboardBounds; 
    [[aNotification.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds]; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    tableview.contentInset = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0); 
    [UIView commitAnimations]; 
    [tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

} 

- (void)keyboardWasHidden:(NSNotification *)aNotification { 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    tableview.contentInset = UIEdgeInsetsZero; 
    [UIView commitAnimations]; 
} 
Questions connexes