2011-04-06 13 views
51

Sé que esto puede sonar como una pregunta tonta, pero he buscado en todas partes. ¿Cómo puedo hacer esto?Agregar/Eliminar UITableViewCell con animación?

sé cómo hacer esto con un método de Swype-eliminar, pero la forma de leva que lo hacen fuera de esa función?

Por favor, publique algunos ejemplos de código.

Gracias!
Coulton

Respuesta

112
[self.tableView beginUpdates]; 
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade]; 
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade]; 
[self.tableView endUpdates]; 

insertIndexPaths es una matriz de NSIndexPaths para ser insertado a su mesa.

deleteIndexPaths es una matriz de NSIndexPaths que se eliminará de su tabla.

Ejemplo formato de matriz de caminos de índice:

NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects: 
     [NSIndexPath indexPathForRow:0 inSection:0], 
     [NSIndexPath indexPathForRow:1 inSection:0], 
     [NSIndexPath indexPathForRow:2 inSection:0], 
     nil]; 
+0

¡Gracias por la respuesta! He utilizado el método de la siguiente manera: '\t \t NSArray * insertIndexPaths = [[NSArray alloc] initWithObjects: [NSIndexPath indexPathForRow: 0 insection: 0], nil]; \t \t \t \t [theTableView beginUpdates]; \t \t [theTableView deleteRowsAtIndexPaths: insertIndexPaths withRowAnimation: UITableViewRowAnimationLeft]; \t \t [theTableView endUpdates]; 'Pero todavía no hizo nada. Estoy pensando que tiene algo que ver con la parte 'theTableView'. Cuando intento 'self.tableView', me dice que no es una estructura o unión ... ¡gracias! – iosfreak

+0

Su controlador de vista necesita subclasificar UITableViewController, o si solo es un UIViewController necesita implementar los protocolos UITableViewDelegate y UITableViewDataSource. Luego, en el constructor de interfaces, agregue una vista de tabla a su vista y vincule sus propiedades de delegado y de fuente de datos a su controlador de vista. – Sabobin

+0

Tengo mi subclase como UIViewController con UITableViewDelegate y DataSource (los tengo vinculados a mi UITableView en mi opinión). ¿Qué más puedo estar haciendo mal? – iosfreak

4

Desea estos dos métodos: insertRowsAtIndexPaths:withRowAnimation: y deleteSections:withRowAnimation: Ambos se detallan en el UITableView documentation.

+0

Gracias por la respuesta. Esto es lo que estoy haciendo: '\t \t \t [processList removeObjectAtIndex: 0]; \t \t \t [theTableView beginUpdates]; \t \t \t [theTableView deleteRowsAtIndexPaths: 0 withRowAnimation: UITableViewRowAnimationFade]; \t \t \t [theTableView deleteSections: 0 withRowAnimation: UITableViewRowAnimationFade]; \t \t \t [theTableView endUpdates]; 'Sin embargo, no sucede nada. – iosfreak

7

En Swift 2.1+

tableView.beginUpdates() 
tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], withRowAnimation: .Fade) 
tableView.insertRowsAtIndexPaths([YourIndexPathYouWantToInsertTo], withRowAnimation: .Fade) 
tableView.endUpdates() 

Además, puede crear un NSIndexPath fácilmente de esta manera:

NSIndexPath(forRow: 0, inSection: 0) 
Cuestiones relacionadas