2014-04-13 17 views
6

Come posso fare senza ricaricare tutta la tabella?Reloading tableView header in ios7

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

    UIView *header; 

    if(!self.searchBar) 
    { 
     self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 10, 270, kRowHeight)]; 
     self.searchBar.delegate = self; 
     self.searchBar.barStyle = UISearchBarStyleDefault; 
     self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
     self.searchBar.barTintColor = [UIColor clearColor]; 
    } 

    if(!self.placeholderSearchBarView) 
    { 
    self.placeholderSearchBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 270, kSearchBarPlaceholderHeight)]; 
    self.placeholderSearchBarView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.7]; 

    } 

    if (self.showSearchBar) 
    { 
     for (UIView *v in self.tableView.tableHeaderView.subviews) 
     { 
      if (v.tag == 3433) 
      { 
       [v removeFromSuperview]; 
      } 
     } 
     header = self.searchBar; 
    } else { 
     header = self.placeholderSearchBarView; 
    } 

    return header; 
} 
+0

So che hai messo di testa, ma si fa a dire [ 'tableViewHeader'] (https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableView/tableHeaderView) o un'intestazione di sezione? Post ha anche un po 'del vostro codice in cui si imposta l'intestazione si prega – Rich

+0

@Rich, aggiorno il mio post, ho una sezione, è sezione di intestazione – user3527777

risposta

4

è necessario utilizzare -reloadSections:withRowAnimation:

Così, quando è necessario aggiornare le intestazioni:

NSIndexSet *headers = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [self.tableView numberOfSections])]; 
[self.tableView reloadSections:headers withRowAnimation:UITableViewRowAnimationAutomatic]; 

Se hai una sola sezione (come hai appena citato nella tua domanda aggiornato) puoi semplicemente chiamare

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic]; 
+0

ricaricata tutto tavolo – user3527777

+0

Fammi aggiornare la risposta con un suggerimento :) – Rich

+0

@ user3527777 prima di tutto, cosa stai cercando di fare con 'tableHeaderView' nel metodo' tableView: viewForHeaderInSection: '? – Rich

9

Devi ricaricare l'intera sezione per ricaricare la h eader.

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation: UITableViewRowAnimationAutomatic]; 

O

Si potrebbe afferrare una maniglia per la vista e aggiornarlo manualmente:

UIView *headerView = [self.tableView headerViewForSection:section]; 
//... update your view properties here 
[headerView setNeedsDisplay]; 
[headerView setNeedsLayout]; 
+0

Con il secondo approccio, come è necessario impostare l'intestazione tableView? Non funziona, lavoro se uso self.tableView.tableHeaderView = headerView; [self.tableView.tableHeaderView setNeedsDisplay]; [self.tableView.tableHeaderView setNeedsLayout]; – user3527777