2011-12-15 16 views
5

Sto tentando di eliminare o nascondere una sezione all'interno di una vista tabella con celle statiche su di essa. Sto cercando di nasconderla nella funzione viewDidLoad. Ecco il codice:Come eliminare sezioni dalla tabella statica tableview

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

    [self.tableView reloadData]; 
} 

Ancora le sezioni appaiono. Sto usando gli storyboard in esso. Puoi aiutarmi per favore? Grazie!

risposta

-2

Sembra che reloadData renda la vista tabella per rileggere dataSource. Dovresti anche rimuovere i dati da dataSource prima di chiamare reloadData. Se si utilizza la matrice, rimuovere l'oggetto desiderato con removeObject: prima di chiamare reloadData.

6

l'ho trovato più conveniente per nascondere sezioni ridefinendo 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

Questo è adatto al mio scopo, ma rimane un po 'di spazio verticale dopo averlo fatto. – guptron

+0

Fa esattamente ciò che l'OP voleva, grande anima! – Tumtum

0

Ecco qui. Questo rimuove anche lo spazio verticale.

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; 
} 
Problemi correlati