【问题标题】:Reloading tableView header in ios7在 ios7 中重新加载 tableView 标头
【发布时间】:2014-05-27 07:09:59
【问题描述】:

如何在不重新加载所有表格的情况下做到这一点?

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

【问题讨论】:

  • 我知道你放了标题,但你的意思是tableViewHeader 还是部分标题?还请在设置标题的位置发布一些代码
  • @Rich,我更新了我的帖子,我有一个部分,它是部分标题

标签: ios uitableview tableview


【解决方案1】:

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

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

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

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

【讨论】:

  • 使用第二种方法我需要如何设置 tableView 标题?它不起作用,如果我使用 self.tableView.tableHeaderView = headerView; [self.tableView.tableHeaderView setNeedsDisplay]; [self.tableView.tableHeaderView setNeedsLayout];
【解决方案2】:

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

【讨论】:

  • @user3527777 在我做之前 - 你想用tableView:viewForHeaderInSection: 方法中的tableHeaderView 做什么?
  • 我的计划是:1.在scrollViewDidScroll中获取滚动偏移tableView并设置标志(向上或向下滚动)2.在scrollViewDidScroll中调用重新加载标题。 3.在viewForHeaderInSection中加载需要的header视图
  • 如果你只有一个部分,我不明白你为什么不直接使用tableHeaderView?然后当你想改变你的标题时,只需将新视图设置为tableHeaderView。您正在使用一个真正适用于具有多个部分的表的部分标题。我认为tableHeaderView 更适合您的需求:)
  • 你的意思是我可以将View设置为tableView.tableHeaderView?是的,我试过了,但我也无法从 scrollViewDidScroll 重新加载它
  • 您不需要重新加载它(除非它包含一些数据 - 然后只需在其上设置新值),只需将另一个视图(self.searchBarself.placeholderSearchBarView 之一)设置为tableHeaderView.
猜你喜欢
  • 1970-01-01
  • 2015-02-20
  • 1970-01-01
  • 2014-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 2012-04-05
相关资源
最近更新 更多