2014-05-13 37 views
3

我做了一个UITableView,并设置了“委托”和“数据源”每次我打电话reloadData时,它进入方法:viewForHeaderInSection:不调用时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; 
} 
+0

我假设你设置你的类都是'dataSource'和''UITableView'的'delegate',对吧? – dasblinkenlight

+0

您也可以使用重载部分方法尝试不同的方法。向我们展示您的表视图页眉和页脚视图数据源回调,以查看是否存在问题。 – J2theC

+0

呀,dataSource和delegate没有错。这种行为只发生在所有部分都关闭时。 – a7md

回答

11

你应该实现heightForHeaderInSection:并设置高度为标头值> 0

+0

谢谢。这可以帮助我... –

0

同样的问题发生与我,是因为的tableview没” t找到高度为标题,但正如我使用自动高度计算xCode 9,我无法给出任何明确的身高值。一些实验后,我得到了解决,我们必须重写此方法,

-(CGFloat)tableView:(UITableView *)tableView 
     estimatedHeightForHeaderInSection:(NSInteger)section 
{ 
     return 44.0f; 
} 

虽然我检查两个选项

  1. 高度自动计算
  2. 自动预测的高度计算

故事板如苹果说,但我仍然得到这个奇怪的错误。

请注意:此错误是只能显示在IOS-10版本不IOS-11版本。也许这是来自xCode的错误。谢谢

相关问题