2011-03-13 20 views
6

he añadido un botón en el UINavigationBar quiero usarlo para borrar todas las filas de uitablviewcómo borrar todas las filas de UITableView

¿cómo puedo hacer eso?

+3

Por favor, aclare lo que quiere decir con "borrar todas las filas". – hennes

+0

Significa que eliminar todo el contenido de la vista de tabla –

Respuesta

1

¿Qué quiere decir exactamente al limpiar la fila? ¿Todavía quieres que estén allí, pero sin texto? Si es así, aquí está el código:

UITableViewCell *cell; 

    NSIndexPath *index; 

     for (int i = 0 ; i < count; i++) { 
      index = [NSIndexPath indexPathForRow:i inSection:0]; 
      cell = [tableView cellForRowAtIndexPath:index]; 
      cell.textLabel.text = @""; 
     } 

Si desea eliminarlos, puede utilizar este código:

[uiTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationFade]; 
+0

Utilicé su segundo código pero SIGABART siempre, ¿qué debo hacer? – Ali

+0

¿Puede agregar un código de muestra de lo que hizo exactamente? – Basel

+0

en el ciclo Incremento y uso: index = [NSIndexPath indexPathForRow: i inSection: 0] ;; [uiTable deleteRowsAtIndexPaths: [NSArray arrayWithObject: index] withRowAnimation: UITableViewRowAnimationFade]; pero después de la iteración seccont se bloquea – Ali

2

Debe haber un método UITableView delegado que puebla el control UITableView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

Encuentra este método y cambia el origen de los datos de cada celda. Si actualmente está utilizando indexPath para acceder a una matriz de valores para rellenar sus celdas tableView, puede cambiar programáticamente a una matriz de valores vacíos, o incluso simplemente devolver celdas vacías de este método cuando la condición sea correcta.

+0

Y también necesita llamar a reloadData en la vista de tabla. – hennes

+0

¡buena captura! :-) –

0

El accidente se produce porque es necesario para restablecer número si el número de filas en el -

-(NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 
    return [tempArray count]; 
} 

Una forma es utilizar una variable de la bandera de la siguiente manera:

-(NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 
    if (deleting) 
     return rowcount; 
    else 
     return [tempArray count]; 
} 

También es necesario modificar el borrando código de la siguiente manera:

deleting = YES; 
    for (i = [uiTable numberOfRowsInSection:0] - 1; i >=0 ; i--) 
    { 
    NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0]; 
    [uiTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationFade]; 

    rowcount = i; 
    } 

    deleting = NO; 
    [uiTable reloadData]; 

en su archivo .h

BOOL deleting; 
NSInteger rowcount; 

Espero que esto ayude ...

2

simple .. establecer el origen de datos (NSArray o NSDictionary) a cero y [self.tableView reloadData]!

+0

¿no eliminaría simplemente la matriz y dónde seguirían existiendo los objetos? si es así, entonces no es una buena solución, ya que crea pérdidas de memoria ... En su lugar, debe probar NSMutableArray * arrayThatYouCanRemoveObjects = [NSMutableArray arrayWithArray: your_array]; [arrayThatYouCanRemoveObjects removeObjectAtIndex: your_object_index]; [versión de su_array]; your_array = [[NSArray arrayWithArray: arrayThatYouCanRemoveObjects] retain]; copiado de http://stackoverflow.com/questions/1728475/nsarray-remove-item-from-array – yunas

12

Un poco tarde ... pero intente esto:

Dónde quiere recoger la mesa:

// clear table 
clearTable = YES; 
[table reloadData]; 
clearTable = NO; 

Esta función debe ser similar:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    if(clearTable) 
     return 0; 

    (...) 
} 
1

Antes vuelve a cargar la tabla elimina todos los objetos de su matriz tableView (La matriz que llenó su tableView) así:

[myArray removeAllObjects]; 
[tableView reloadData]; 
0

El rowcount = i debe ir antes de la llamada.

[uiTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] 
withRowAnimation:UITableViewRowAnimationFade]; 

De lo contrario, se bloqueará.

Cuestiones relacionadas