2009-10-26 15 views
5

Necesito agregar una nueva fila en la vista de tabla cuando selecciono una fila en la tabla.Agregar una fila dinámicamente en TableView of iphone

¿Supongo que debo usar el siguiente método?

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

¿Qué debo escribir dentro del método para agregar una nueva fila.?

Por favor, ayúdame,

Gracias de antemano Shibin.

Respuesta

2

Si está completando su mesa de una matriz, puede simplemente añadir un elemento a la matriz y llame [myTable reloadData]

+0

no, no estoy cargando la nueva fila de datos de una matriz. lo que necesito es agregar una nueva fila cuando selecciono una fila en particular en la tabla. simplemente se adjunta a mi tabla existente. – smakstr

+0

Hmm. ¿Y cuál es la fuente de su contenido de vista de tabla? – Morion

+1

Puede simplemente cambiar el número de elementos devueltos por numberOfRowsInSection y actualizar los datos devueltos por cellForRowAtIndexPath de forma adecuada. Luego llame a reloadData como se indica arriba. –

13

Cuando agrego una fila de forma dinámica, utilizo insertRowsAtIndexPaths: aquí es una función de ejemplo

- (void) allServersFound { 
    // called from delegate when bonjourservices has listings of machines: 
    bonjourMachines = [bonjourService foundServers]; // NSArray of machine names; 

    NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 

    int i = 0; 
    for (NSArray *count in bonjourMachines) { 
     [tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]]; 
    } 

    [[self tableView] beginUpdates]; 
    [[self tableView] insertRowsAtIndexPaths:(NSArray *)tempArray withRowAnimation:UITableViewRowAnimationNone]; 
    [[self tableView] endUpdates]; 

    [tempArray release]; 
} 
1

datos de recarga es agradable y simple, pero no se animan por lo que yo puedo decir ...

0

reloadData puede añadir fila en vista teble pero si quieres algo de animación cuando la fila añadir entonces usted debe utilizar estas líneas de código.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
     [tempArray addObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]]; 

     [mainArray insertObject:@"new row" atIndex:indexPath.row+1]; 

     [[self tableView] beginUpdates]; 
     [[self tableView] insertRowsAtIndexPaths:(NSArray *)tempArray withRowAnimation:UITableViewRowAnimationFade]; 
     [[self tableView] endUpdates]; 

} 
Cuestiones relacionadas