Actualizado con Swift 4
Después de algunos experimentos, también basados en respuestas anteriores, tengo la conclusión de que el mejor comportamiento se puede lograr de 2 maneras: (casi idéntico en la práctica)
// First Solution: delegate of the table View
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
}
// Second Solution: With the life cycle of the view.
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
let selectedRow: IndexPath? = tableView.indexPathForSelectedRow
if let selectedRow = selectedRow {
tableView.deselectRow(at: selectedRow, animated: false)
}
}
Personalmente, estoy adoptando la primera solución, porque es simplemente más conciso. Otra posibilidad, si necesita un poco de animación cuando regrese a su tableView, es usar viewWillAppear
:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let selectedRow: IndexPath? = _view.tableView.indexPathForSelectedRow
if let selectedRow = selectedRow {
_view.tableView.deselectRow(at: selectedRow, animated: true)
}
}
Por último, pero no menos importante, si está utilizando un UITableViewController, también puede aprovechar la propiedad clearsSelectionOnViewWillAppear .
Alessandro Francucci
fuente
deselectRowAtIndexPath
a mi viewDidAppear, si selecciono la fila aparece una nueva vista.-deselectRowAtIndexPath:animated:
método en su propiedad tableView desde-viewDidAppear
. Sin embargo, si tiene una vista de tabla en una subclase de UIViewController, debe llamar-deselectRowAtIndexPath:animated:
desde-viewDidAppear
usted mismo. :)-viewWillAppear:
eso lo rompió. Agregar una llamada para[super viewWillAppear:animated]
que funcione nuevamente.UITableViewController
'sclearsSelectionOnViewWillAppear
propiedad se establece enYES
(que es el valor por defecto), y no se ha impedido[super viewWillAppear]
de ser llamado.