viewForHeaderInSection:调用reloadData:时不调用

时间:2014-05-13 17:31:17

标签: objective-c ios7 uitableview

我制作了UITableView并设置了"委托"和"数据源"每当我拨打reloadData时,它都会进入方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.headersList count];
}

方法:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    SectionInfo *headerInfo = (self.headerInfoArray)[section];
NSInteger numOfObjectsInSection = [[headerInfo.list objectsInList] count];
    return headerInfo.open ? numOfObjectsInSection : 0;
}

然后停止!它没有进入ViewForHeaderInSection:方法。我也实现了这个方法:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SECTION_HEADER_HEIGHT;
}
  • 知道我使用开/关部分功能!所以首先关闭所有部分,每个部分的行数为0,但返回的部分数是正确的(当打开一个部分时,行数会更新)。
  • 显示标题视图的唯一方法是等待一段时间,直到它自动重新加载!或者我向上或向下滑动!

viewForHeaderInSection方法:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UISectionHeaderView *sectionHeaderView = [[UISectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, SECTION_HEADER_HEIGHT)];

    SectionInfo *sectionInfo = (self.headerInfoArray)[section];
    sectionHeaderView.open = sectionInfo.open;
    sectionInfo.headerView = sectionHeaderView;

    sectionHeaderView.titleLabel.text = [NSString stringWithFormat:@"%@ (%lu)",sectionInfo.list.title, (unsigned long)[sectionInfo.list.objectsInList count]];
    sectionHeaderView.section = section;
    sectionHeaderView.delegate = self;

    return sectionHeaderView;
}

2 个答案:

答案 0 :(得分:12)

您应该实施heightForHeaderInSection:并将标头的高度设置为值> 0.

答案 1 :(得分:1)

我遇到同样的问题,因为 tableview 没有找到标题的高度,但我正在使用自动高度计算 > xCode 9 ,我不能给出任何明确的高度值。经过一些实验,我得到了解决方案,我们必须将此方法重写为,

DT[, .N * NA^is.na(g), by = g]

虽然我有选中这两个选项

  1. 自动高度计算
  2. 自动估算高度计算
  3. 苹果说,

    来自故事板,但我仍然遇到了这个奇怪的错误。

    请注意:此错误仅在 IOS-10 版本上显示,而不在 IOS-11 版本上显示。也许这是xCode的一个bug。感谢

相关问题