viewForHeaderInSection:调用reloadData:时不调用

a7m*_*7md 3 objective-c uitableview ios7

我做了一个UITableView并设置了"委托"和"数据源",每次调用时reloadData,它都会进入方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.headersList count];
}
Run Code Online (Sandbox Code Playgroud)

方法:

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

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

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SECTION_HEADER_HEIGHT;
}
Run Code Online (Sandbox Code Playgroud)
  • 知道我使用开/关部分功能!所以首先关闭所有部分,每个部分的行数为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;
}
Run Code Online (Sandbox Code Playgroud)

Chi*_*ara 12

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