2011-12-15 15 views
5

Estoy tratando de eliminar u ocultar una sección dentro de una tabla vista con celdas estáticas en ella. Estoy tratando de ocultarlo en la función viewDidLoad. Aquí está el código:Cómo eliminar secciones de la tabla de tablas estática

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.tableView beginUpdates]; 
    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:YES]; 
    [self.tableView endUpdates]; 

    [self.tableView reloadData]; 
} 

Todavía aparecen las secciones. Estoy usando guiones gráficos. ¿Puedes ayudarme por favor? ¡Gracias!

Respuesta

-2

Parece que reloadData hace que la vista de tabla vuelva a leer dataSource. También debe eliminar datos de dataSource antes de llamar al reloadData. Si está utilizando una matriz, elimine el objeto que desee con removeObject: antes de llamar al reloadData.

6

Encontré más conveniente ocultar secciones anulando numberOfRowsInSection.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 1) 
     // Hide this section 
     return 0; 
    else 
     return [super tableView:self.tableView numberOfRowsInSection:section]; 
} 
+0

Esto se ajustaba a mi propósito, pero sigue habiendo un poco de espacio vertical después de hacer esto. – guptron

+0

Hace exactamente lo que OP quería, gran soultion! – Tumtum

0

Aquí tienes. Este también elimina el espacio vertical.

NSInteger sectionToRemove = 1; 
CGFloat distance = 10.0f; // minimum is 2 since 1 is minimum for header/footer 
BOOL removeCondition; // set in viewDidLoad 

/** 
* You cannot remove sections. 
* However, you can set the number of rows in a section to 0, 
* this is the closest you can get. 
*/ 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    if (removeCondition && section == sectionToRemove) 
    return 0; 
    else 
    return [super tableView:self.tableView numberOfRowsInSection:section]; 
} 

/** 
* In this case the headers and footers sum up to a longer 
* vertical distance. We do not want that. 
* Use this workaround to prevent this: 
*/ 
- (CGFloat)tableView:(UITableView *)tableView 
    heightForFooterInSection:(NSInteger)section { 

    return removeCondition && 
       (section == sectionToRemove - 1 || section == sectionToRemove) 
      ? distance/2 
      : distance; 
} 

- (CGFloat)tableView:(UITableView *)tableView 
    heightForHeaderInSection:(NSInteger)section { 

    return removeCondition && 
       (section == sectionToRemove || section == sectionToRemove + 1) 
      ? distance/2 
      : distance; 
} 
Cuestiones relacionadas