2011-05-06 22 views
8

Estoy tratando de obtener un código de muestra sobre cómo agregaría filas a un UITableView existente. Estoy tratando de usar la función insertRowsAtIndexPaths:.Agregar filas a la sección UITableView existente

[tableView insertRowsAtIndexPaths:addindexes withRowAnimation:UITableViewRowAnimationTop]; 

Alguna idea de cómo funcionan estos índices y cómo puedo añadir a una sección existente o, si no tengo una sección, a continuación, crear una nueva sección?

Respuesta

11

Hay que crear una matriz de indexpaths como -

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 

por ejemplo, si desea insertar segunda fila en la sección 0 y ya cuentan con 1 fila en la sección 0, entonces crear una ruta del índice de la fila 1 de sección 0 y llame a insertRowsAtIndexPaths en la vista de tabla, insertará la 2ª fila en la sección.

Si no hay una sección en la vista de tabla, entonces debería usar un int para la fuente de datos para dar ninguna de las secciones vista de tabla, en este uso -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return noOfSection; //here section is as int which is initially zero 
} 

e inicialmente su "noOfSection" int será cero entonces no habrá sección en la tabla, luego cuando quiera agregar sección aumente su valor int en 1 y llame al [tableView reloadData];

6

Usted necesita manejar estas funciones si va a insertar fila y sección en una vista de tabla.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return noOfSections; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [[noOfRows objectForIndex:section] intValue]; 
} 

también si está actualizando la tabla de vista, es recomendable que utilice beginUpdates y endUpdates método.

esta es la forma de insertar una nueva fila y sección.

noOfSections++; 
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:3] withRowAnimation:UITableViewRowAnimationTop]; 


// update your array before calling insertRowsAtIndexPaths: method 
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:2]] 
         withRowAnimation:UITableViewRowAnimationTop]; 

verifique el código de muestra proporcionado por la manzana.

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableView_Class/Reference/Reference.html

Cuestiones relacionadas