2011-07-13 12 views
15

Tengo un UIViewController que gestiona UISearchBar y UITableView. He leído que Apple desaconseja que varios UIViewControllers administren parte de su aplicación, por lo que no utilicé UITableViewController para administrar el UITableView. En cambio, implementé el protocolo UITableViewDelegate y UITableViewDataSource en mi propio UIViewController.¿Cómo cambiar clearsSelectionOnViewWillAppear cuando no se utiliza UITableViewController?

Mi pregunta es, dado que ya no estoy usando UITableViewController, ¿cómo realmente puedo cambiar el comportamiento de clearsSelectionOnViewWillAppear? Esta propiedad es parte de UITableViewController.

Respuesta

40

simplemente llamando

[myTableView deselectRowAtIndexPath:[myTableView indexPathForSelectedRow] animated:YES]; 

en su método viewWillAppear:.

+1

bien, veo su punto. Entonces, esencialmente, tengo que replicar el comportamiento manualmente. Eso es un poco rudo, especialmente si hay algo más que proporciona UITableViewController que es más difícil de lograr. – pixelfreak

+0

Puse esto en mi didSelectRowAtIndexPath, y funcionó para deseleccionar la fila. 'tableView.deselectRowAtIndexPath (indexPath, animated: true)' – Robert

4

Probablemente esté anulando el método viewWillAppear:animated y omitiendo la llamada [super viewWillAppear:animated].

4

Aquí el código Swift:

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 
    if let indexPath = tableView.indexPathForSelectedRow() { 
     tableView.deselectRowAtIndexPath(indexPath, animated: true) 
    } 
} 
-2

Por defecto, la fila seleccionada permanece seleccionada al volver al controlador. Al agregar un comando para anular la selección de la fila en viewWillAppear, puede estar seguro de que no se seleccionarán filas al regresar a la pantalla.

Aquí está el código Swift 3.2 que trabajó para mí:

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    if let indexPath = myTable.indexPathForSelectedRow { 
     myTable.deselectRow(at: indexPath, animated: true) 
    } 
} 
+0

sería bueno agregar más detalles sobre por qué funciona. – gdbj

+0

@gdbj He editado la publicación, hágamelo saber si tiene alguna pregunta. – bb90

+1

¿En qué se diferencia tu respuesta de la de Darko? – gdbj

Cuestiones relacionadas