Como faço para selecionar programaticamente uma linha UITableView
para que
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
é executado? selectRowAtIndexPath
apenas realçará a linha.
Da documentação de referência:
Chamar esse método não faz com que o delegado receba uma
tableView:willSelectRowAtIndexPath:
outableView:didSelectRowAtIndexPath:
nem envia notifications deUITableViewSelectionDidChangeNotification
para observadores.
O que eu faria é:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self doSomethingWithRowAtIndexPath:indexPath]; }
E então, de onde você queria chamar selectRowAtIndexPath, você chamava doSomethingWithRowAtIndexPath. Além disso, você também pode chamar selectRowAtIndexPath se quiser que o feedback da interface do usuário aconteça.
Como Jaanus disse:
Chamar esse método (-selectRowAtIndexPath: animated: scrollPosition 🙂 não faz com que o delegate receba uma mensagem tableView: willSelectRowAtIndexPath: ou tableView: didSelectRowAtIndexPath:, nem enviará notifications de UITableViewSelectionDidChangeNotification para observadores.
Então você só precisa chamar o método de delegate
.
Por exemplo:
Versão 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)
Versão ObjectiveC:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
Versão 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)
UITableView’s selectRowAtIndexPath: animado: scrollPosition: deve fazer o truque.
Basta passar UITableViewScrollPositionNone
para scrollPosition e o usuário não verá nenhum movimento.
Você também deve poder executar manualmente a ação:
[theTableView.delegate tableView:theTableView didSelectRowAtIndexPath:indexPath]
after you selectRowAtIndexPath:animated:scrollPosition:
assim, o destaque acontece, assim como qualquer lógica associada.
Se você quiser selecionar alguma linha isso irá ajudá-lo
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [someTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
Isso também realçará a linha. Então delegue
[someTableView.delegate someTableView didSelectRowAtIndexPath:indexPath];
Solução Swift 3.0
Selecione a linha
let indexPath = IndexPath(row: 0, section: 0) tblView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom) myTableView.delegate?.tableView!(myTableView, didSelectRowAt: indexPath)
DeSelect Row
let deselectIndexPath = IndexPath(row: 7, section: 0) tblView.deselectRow(at: deselectIndexPath, animated: true) tblView.delegate?.tableView!(tblView, didDeselectRowAt: indexPath)
Use essa categoria para selecionar uma linha da tabela e executar uma sequência depois de um atraso.
Chame isso no seu método viewDidAppear
:
[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
Existem dois methods diferentes para as plataformas iPad e iPhone, então você precisa implementar ambos:
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];