2011-05-07 14 views
16

Me preguntaba si hay alguna manera de que mi código "toque" una celda en mi UITableView para reproducir el comportamiento especificado en el método delegado - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath.¿Cómo "presionar" programáticamente una celda UITableView?

Supongo que en otras palabras, ¿es posible invocar el método delegado - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath de forma programática?

Respuesta

19

¿No puede poner cualquier lógica en didSelectRowAtIndexPath en un método separado y simplemente llama a ese método desde didSelectRowAtIndexPath y donde quiera que desee llamar al mismo código?

+0

Gracias, eso es realmente lo que hice y trabajo ... – cgossain

+1

Sí, pero la celda no se resalta como lo hace cuando toca. Alguna idea allí? –

+1

@BeemerFan: Simplemente llame desde el método que llama al nuevo método común: [self.tableView selectRowAtIndexPath: myIndexPath animated: false scrollPosition: UITableViewScrollPositionNone]; – Rob

0

Es solo un método. Continúa e invoca como si invocases cualquier otro método.

+4

Que en realidad es un método de instancia, no es un método de clase - pero bueno, eso podría ser nitpicking: D. – Till

35

si usted quiere tener la celda seleccionada, es decir, resaltar una celda específica:

//select first row of first section 
NSIndexPath* selectedCellIndexPath= [NSIndexPath indexPathForRow:0 inSection:0]; 
[self.tableView selectRowAtIndexPath:selectedCellIndexPath animated:false scrollPosition:UITableViewScrollPositionMiddle]; 

si desea activar, además, las acciones en su método didSelectRowAtIndexPath, es necesario llamar manualmente el método delegado, ganó' t suceder automáticamente:

[self tableView:self.tableView didSelectRowAtIndexPath:selectedCellIndexPath]; 
+0

La segunda línea de código hizo el trabajo para mí, gracias. –

+0

Sin embargo, la llamada a didSelectRowAtIndexPath no desencadena los cambios. – Andy

+0

Sí lo hace. Funciona para mi. – Tander

0

Esto es una actualización de 6.1. Cuando usa segues, todo el consejo es bueno, pero también debe invocar el cambio. Por lo tanto - para agregar a los consejos y resumirlo

// ... assuming we just added a new row - which is one use of what this thread is trying to do 

/* get new count - this is the row we are going to highlight - 0 based */ 
int newIndex = [dataArrayUnderlyingTable count]-1; 

/* make it look pretty by highlighting it */ 
    NSIndexPath *nip = [NSIndexPath indexPathForRow:newIndex inSection:0]; 
    [[self tableView] selectRowAtIndexPath:nip animated:YES scrollPosition:UITableViewScrollPositionBottom]; 

/* run the code in the following method so it is not missed */ 
    [self tableView:[self tableView] didSelectRowAtIndexPath:nip]; 

/* now 'tap' on it ... or simulate it */ 
    [self performSegueWithIdentifier: @"viewChildDetails" sender: self]; 
1

Swift 3.2 Solución

let indexPath = IndexPath(row: 2, section: 0) // change row and section according to you 
tblView.selectRow(at: indexPath, animated: true) 
tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath) 
+0

Parece que se tomó aquí https://stackoverflow.com/a/40590050/5790492! –

Cuestiones relacionadas