重新加载ios7中的tableView标头

我怎样才能不重新加载所有表?

-(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; } 

您必须重新加载整个部分才能重新加载标题。

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

要么

您可以抓取视图的句柄并手动更新:

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

你需要使用-reloadSections:withRowAnimation:

所以当你需要更新你的头文件时:

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

如果你只有一个部分(就像你刚刚提到的更新的问题),你可以打电话

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