2010-06-26 33 views
28

¿Alguien me puede ayudar con UITableView problema de animación?UITableView agregar celda Animación

Por defecto tenemos animación para eliminar celdas y reordenar celdas en UITableView.

¿Podemos tener una celda de adición animada? Si es así, cómo hacerlo.

He comprobado Three20, no entiendo como no lo ha hecho twitter la tabla ampliar la animación bajo MiPerfil> ReTweets.

Quiere probarlo sin Three20 frameowrk, usando la animación existente en UITableView.

Respuesta

60

Se puede utilizar el siguiente método UITableView:

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 

Ejemplo con self.dataSource ser un array mutable y su tabla sólo tiene 1 sección:

[self.dataSource addObject:@"New Item"]; 
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:[self.dataSource count]-1 inSection:0]; 
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 

Nota: Restado 1 de recuento fuente de datos ya que NSIndexPath tiene índice cero.

+0

gracias por el código, ¿me puede dar una muestra de trabajo? o Estoy un poco confundido con la forma de actualizar la fuente de datos mientras hago la inserción. – Ameya

+0

anterior Simplemente estaría agregando un registro a la fuente de datos (NSMutableArray) y llamando [self.tableView reloadData]; Pero eso no traería animadamente el nuevo elemento. – Ameya

+1

solo un pequeño cambio en nuestro código @JK por favor edite su respuesta, espero que ayude a alguien que busque este tipo de solución. [tableView insertRowsAtIndexPaths: [NSArray arrayWithObjects: [NSIndexPath indexPathForRow: [self.recuento dataSource] insection: 0], nil] withRowAnimation: UITableViewRowAnimationRight]; – Ameya

10

Swift

Ejemplo matriz que es la fuente de datos para la vista de tabla

var myArray = ["Cow", "Camel", "Sheep", "Goat"] 

Lo siguiente añadir una fila con la animación en la parte superior de la vista de tabla.

// add item to current array 
myArray.insert("Horse", atIndex: 0) 

// insert row in table 
let indexPath = NSIndexPath(forRow: 0, inSection: 0) 
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade) 

múltiples actualizaciones

Si lo que necesita hacer múltiples inserciones y/o supresiones luego rodearlos con beginUpdates() y endUpdates().

tableView.beginUpdates() 
tableView.insertRowsAtIndexPaths([addIndexPath1, addIndexPath2, ...], withRowAnimation: .Fade) 
tableView.deleteRowsAtIndexPaths([deleteIndexPath1, deleteIndexPath2, ...], withRowAnimation: .Fade) 
tableView.endUpdates() 

Otras lecturas

+1

estoy usando el código para insertar las filas que necesito insertarán, sin embargo, no importa qué tipo de animación ESCOJO siempre anima de la misma manera (es decir, desvanecimiento, botom, izquierda, todos tienen el mismo comportamiento) . ¿Alguna idea de por qué sucede esto? –

+0

@GeorgeAvgoustis, no he trabajado en esto por un tiempo, así que no sé. Lo pondré en mi lista de cosas para revisar, pero es posible que no lo haga por un tiempo. Si lo resuelves primero, por favor deja otro comentario. – Suragch

0

Use reloadRowsAtIndexPaths: indexPath

tableView.beginUpdates() 
    [tableView reloadRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationAutomatic]; 
    tableView.endUpdates() 

espero que esto le ayudará a ..

Cuestiones relacionadas