60 votes

UITableView, défilement vers le bas sur recharger?

J'ai un UITableView avec des cellules qui sont mises à jour dynamiquement. Tout fonctionne bien, à l'exception de l'appel de reload (voir ci-dessous) pour actualiser les cels du tableau. J'aimerais que le tableau défile vers le bas pour afficher les nouvelles entrées.

 - (void)reloadTable:(NSNotification *)notification {
    NSLog(@"RELOAD TABLE ...");
    [customTableView reloadData];
    // Scroll to bottom of UITable here ....
}
 

J'avais prévu d'utiliser scrollToRowAtIndexPath: atScrollPosition: animated: mais j'ai ensuite remarqué que je n'avais pas accès à indexPath. Est-ce que quelqu'un sait comment faire cela, ou d'un rappel de délégué que je pourrais utiliser?

122voto

Max Points 12661

Utilisation:

 NSIndexPath* ipath = [NSIndexPath indexPathForRow: cells_count-1 inSection: sections_count-1];
[tableView scrollToRowAtIndexPath: ipath atScrollPosition: UITableViewScrollPositionTop animated: YES];
 

Ou vous pouvez spécifier l'index de section manuellement (Si une section => index = 0).

34voto

mkral Points 2313
-(void)scrollToBottom{

        [self.tableView scrollRectToVisible:CGRectMake(0, self.tableView.contentSize.height - self.tableView.bounds.size.height, self.tableView.bounds.size.width, self.tableView.bounds.size.height) animated:YES];

}

10voto

ytbryan Points 186
//In swift 

var iPath = NSIndexPath(forRow: self.tableView.numberOfRowsInSection(0)-1, 
                        inSection: self.tableView.numberOfSections()-1)
self.tableView.scrollToRowAtIndexPath(iPath, 
                                      atScrollPosition: UITableViewScrollPosition.Bottom,                     
                                      animated: true)

5voto

wzbozon Points 2851

C'est une autre solution, qui a bien fonctionné dans mon cas, lorsque la hauteur de la cellule est grande.

 - (void)scrollToBottom
{
    CGPoint bottomOffset = CGPointMake(0, _bubbleTable.contentSize.height - _bubbleTable.bounds.size.height);
    if ( bottomOffset.y > 0 ) {
        [_bubbleTable setContentOffset:bottomOffset animated:YES];
    }
}
 

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X