Per solamente selezionare la prima cella la prima volta che la tabella è caricata, si potrebbe pensare che l'utilizzo viewDidLoad
è il posto giusto per andare, ma, in quel momento di esecuzione, la tabella non ha caricato il suo contenuto, quindi non funzionerà (e probabilmente bloccherà l'app dal momento che il NSIndexPath
punterà a una cella inesistente).
Una soluzione alternativa consiste nell'utilizzare una variabile che indica che la tabella è stata caricata prima e che esegue il lavoro di conseguenza.
@implementation MyClass {
BOOL _tableHasBeenShownAtLeastOnce;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_tableHasBeenShownAtLeastOnce = NO; // Only on first run
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (! _tableHasBeenShownAtLeastOnce)
{
_tableHasBeenShownAtLeastOnce = YES;
BOOL animationEnabledForInitialFirstRowSelect = YES; // Whether to animate the selection of the first row or not... in viewDidAppear:, it should be YES (to "smooth" it). If you use this same technique in viewWillAppear: then "YES" has no point, since the view hasn't appeared yet.
NSIndexPath *indexPathForFirstRow = [NSIndexPath indexPathForRow:0 inSection: 0];
[self.tableView selectRowAtIndexPath:indexPathForFirstRow animated:animationEnabledForInitialFirstRowSelect scrollPosition:UITableViewScrollPositionTop];
}
}
/* More Objective-C... */
@end
fonte
2015-10-05 19:40:51
Sto anche cercando di capire come fare questo. Deve essere impostato dopo che le funzioni di UITableViewDataSource sono state chiamate in modo che la vista tabella abbia qualcosa in essa. UITableView non sembra avere alcun richiamo che si attiva dopo il caricamento dei dati. – jpsimons