Come faccio a selezionare a livello di UITableViewcodice una riga in modo che
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
viene eseguito? selectRowAtIndexPathevidenzierà solo la riga.
Come faccio a selezionare a livello di UITableViewcodice una riga in modo che
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
viene eseguito? selectRowAtIndexPathevidenzierà solo la riga.
Risposte:
Dalla documentazione di riferimento:
La chiamata a questo metodo non fa in modo che il delegato riceva un
tableView:willSelectRowAtIndexPath:o untableView:didSelectRowAtIndexPath:messaggio, né inviaUITableViewSelectionDidChangeNotificationnotifiche agli osservatori.
Quello che vorrei fare è:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self doSomethingWithRowAtIndexPath:indexPath];
}
Quindi, da dove si desidera chiamare selectRowAtIndexPath, si chiama invece doSomethingWithRowAtIndexPath. Inoltre, puoi anche chiamare selectRowAtIndexPath se desideri che avvenga il feedback dell'interfaccia utente.
Come disse Jaanus:
Chiamare questo metodo (-selectRowAtIndexPath: animato: scrollPosition :) non fa in modo che il delegato riceva un tableView: willSelectRowAtIndexPath: o tableView: didSelectRowAtIndexPath: message, né invierà notifiche UITableViewSelectionDidChangeNotification agli osservatori.
Quindi devi solo chiamare il delegatemetodo da solo.
Per esempio:
Versione Swift 3:
let indexPath = IndexPath(row: 0, section: 0);
self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
self.tableView(self.tableView, didSelectRowAt: indexPath)
Versione ObjectiveC:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath
animated:YES
scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
Versione Swift 2.3:
let indexPath = NSIndexPath(forRow: 0, inSection: 0);
self.tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
self.tableView(self.tableView, didSelectRowAtIndexPath: indexPath)
SelectRowAtIndexPath di UITableView : animato: scrollPosition: dovrebbe fare il trucco.
Basta passare UITableViewScrollPositionNoneper scrollPosition e l'utente non vedrà alcun movimento.
Dovresti anche essere in grado di eseguire manualmente l'azione:
[theTableView.delegate tableView:theTableView didSelectRowAtIndexPath:indexPath]
dopo di te, selectRowAtIndexPath:animated:scrollPosition:così avviene l'highlight e qualsiasi logica associata.
se vuoi selezionare qualche riga, questo ti aiuterà
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[someTableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
Ciò evidenzierà anche la riga. Quindi delegare
[someTableView.delegate someTableView didSelectRowAtIndexPath:indexPath];
Soluzione Swift 3/4/5
Seleziona riga
let indexPath = IndexPath(row: 0, section: 0)
tblView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
myTableView.delegate?.tableView!(myTableView, didSelectRowAt: indexPath)
Deseleziona riga
let deselectIndexPath = IndexPath(row: 7, section: 0)
tblView.deselectRow(at: deselectIndexPath, animated: true)
tblView.delegate?.tableView!(tblView, didDeselectRowAt: indexPath)
Esistono due metodi diversi per piattaforme iPad e iPhone, quindi è necessario implementarli entrambi:
segue.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
// Selection handler (for horizontal iPad)
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
// Segue (for iPhone and vertical iPad)
[self performSegueWithIdentifier:"showDetail" sender:self];Utilizzare questa categoria per selezionare una riga della tabella ed eseguire un determinato seguito dopo un ritardo.
Chiamalo nel tuo viewDidAppearmetodo:
[tableViewController delayedSelection:withSegueIdentifier:]
@implementation UITableViewController (TLUtils)
-(void)delayedSelection:(NSIndexPath *)idxPath withSegueIdentifier:(NSString *)segueID {
if (!idxPath) idxPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self performSelector:@selector(selectIndexPath:) withObject:@{@"NSIndexPath": idxPath, @"UIStoryboardSegue": segueID } afterDelay:0];
}
-(void)selectIndexPath:(NSDictionary *)args {
NSIndexPath *idxPath = args[@"NSIndexPath"];
[self.tableView selectRowAtIndexPath:idxPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
if ([self.tableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)])
[self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:idxPath];
[self performSegueWithIdentifier:args[@"UIStoryboardSegue"] sender:self];
}
@end