在ios7中重新加载tableView标头

时间:2014-04-13 09:47:28

标签: ios uitableview 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;
}

2 个答案:

答案 0 :(得分:12)

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

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

OR

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

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

答案 1 :(得分:5)

您需要使用-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];