11

How can I do it without reloading all table?

-(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;
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3527777
  • 135
  • 1
  • 1
  • 8
  • I know you've put header, but do you mean [`tableViewHeader`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableView/tableHeaderView) or a section header? Also post some of your code where you setup the header please – Rich Apr 13 '14 at 10:12
  • @Rich, I update my post, I have one section, it section header – user3527777 Apr 13 '14 at 10:17

2 Answers2

12

You have to reload the whole section to reload the header.

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

OR

You could grab a handle to the view and update it manually:

UIView *headerView = [self.tableView headerViewForSection:section];
//... update your view properties here
[headerView setNeedsDisplay];
[headerView setNeedsLayout];
Oliver Atkinson
  • 7,970
  • 32
  • 43
  • With second approach how do I need set tableView header? It not work, work if I use self.tableView.tableHeaderView = headerView; [self.tableView.tableHeaderView setNeedsDisplay]; [self.tableView.tableHeaderView setNeedsLayout]; – user3527777 Apr 13 '14 at 10:56
5

You need to use -reloadSections:withRowAnimation:

So when you need to update your headers:

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

If you've only got one section (as you just mentioned in your updated question) you can just call

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
Rich
  • 8,108
  • 5
  • 46
  • 59
  • @user3527777 acutally before I do - what are you trying to do with the `tableHeaderView` in the `tableView:viewForHeaderInSection:` method? – Rich Apr 13 '14 at 10:59
  • My plan was: 1.get scroll offset tableView in scrollViewDidScroll and set flag (scroll to up or to down) 2. in scrollViewDidScroll call reload header. 3. in viewForHeaderInSection load needed view in header – user3527777 Apr 13 '14 at 11:08
  • I can't see why you don't just use `tableHeaderView` if you've only got one section? Then when you want to change your header, just set the new view to `tableHeaderView`. You're using a section header which is really for a table with multiple sections. I think the `tableHeaderView` is more appropriate for your needs :) – Rich Apr 13 '14 at 11:10
  • you mean that I can set View to tableView.tableHeaderView? yep, I tried it but I also can not reload it from scrollViewDidScroll – user3527777 Apr 13 '14 at 11:14
  • You wouldn't need to reload it (unless it contains some data - then just set the new values on it), just set the other view (one of `self.searchBar` or `self.placeholderSearchBarView`) to `tableHeaderView`. – Rich Apr 13 '14 at 11:15
  • I can not do it, I need stick header – user3527777 Apr 13 '14 at 11:34