he encontrado una solución. UITableViewCell
se niega a dibujar el control de reorden a menos que esté en modo de edición. Afortunadamente, UITableViewCell
y UITableView
rastrean la edición por separado, y lo más importante, UITableView
realmente maneja el reordenamiento independientemente de su propio modo de edición. Solo tenemos que engañar a las células para que dibujen los controles de reorden y estamos en casa libres.
Subclase UITableViewCell
así:
class ReorderTableViewCell: UITableViewCell {
override var showsReorderControl: Bool {
get {
return true // short-circuit to on
}
set { }
}
override func setEditing(editing: Bool, animated: Bool) {
if editing == false {
return // ignore any attempts to turn it off
}
super.setEditing(editing, animated: animated)
}
}
Ahora basta con establecer editing = true
en celdas para las que desee habilitar reordenamiento. Puede hacerlo condicional en -tableView:canMoveRowAtIndexPath:
.
En su tabla de origen vista de datos:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
// Configure the cell...
:
cell.editing = self.tableView(tableView, canMoveRowAtIndexPath: indexPath) // conditionally enable reordering
return cell
}
El único inconveniente es que esto es incompatible con la vista de tabla allowsMultipleSelectionDuringEditing
opción; el control de edición siempre se muestra de forma incorrecta. Una solución alternativa es habilitar la selección múltiple solo durante la edición de la vista de tabla real.
En su controlador de vista de tabla:
override func setEditing(editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
self.tableView.allowsMultipleSelectionDuringEditing = editing // enable only during actual editing to avoid cosmetic problem
}
También, en -viewDidLoad
o su guión gráfico, establecer el valor inicial de allowsMultipleSelectionDuringEditing
a falso.
Establezca el indicador UITableViewCellEditingStyle en cada celda e implemente los métodos de delegado canMoveRowAtIndexPath y canEditRowAtIndexPath UITableViewDataSource. No estoy seguro de si tiene que estar en el modo de edición de forma permanente para hacer esto. – TheBlack