簡體   English   中英

在ios7中重新加載tableView標頭

[英]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;
}

您必須重新加載整個部分才能重新加載標題。

[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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM