繁体   English   中英

viewForHeaderInSection在UITableView中仅被调用一次

[英]viewForHeaderInSection is called only once in UITableView

我已经用尽了Google上的所有功能,尽管我敢肯定这是非常令人尴尬的简单操作。

我试图创建一个UITableView,它代表25个(每个行1行)部分中的(假设)对象。

我正在创建的自定义单元格显示得很好(它们全部25个,以正确的顺序排列)。 我希望将节标题视图显示在每个对象上方。

问题在于仅显示了一个部分标题视图(第一个)。

明智的NSLogging告诉我viewForHeaderInSection仅被调用一次,尽管heightForHeaderInSection每个对象被称为2x。 我从numberOfSectionsInTableView返回25,从numberOfRowsInSection返回1。

我正在通过做UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, viewHeight)]并在其中添加UILabel和UIButton来在viewForHeaderInSection中构造UIView。 我不继承UITableViewHeaderFooterView。 不知道这是否会有所作为。

有任何想法吗?

相关代码:

节数和行数:

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

- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

行的高度和单元格:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 390;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
                        object:(PFObject *)object
{
    static NSString *CellIdentifier = @"CustomCell";    
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // add custom content to cell

    return cell;
}

标头的高度和视图:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSLog(@"in viewForHeaderInSection %ld", (long)section); // I only see this once
    NSLog(@"return view for section %ld", section);         // this is 0 when I see it

    // gather content for header
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, viewHeight)];
    // add content to header (displays correctly)

    return headerView;
}

虽然为时已晚。 经过30分钟的战斗,我在xCode 8.1中找到了解决方案。 我说这可能对其他人有帮助。

//添加波纹管委托方法后 ,我多次发现了这个问题

 - (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section 
{
   return 30.0;
} 

这没有解决问题,但是在格式化上面的代码时,我想到了一个解决方案:如果每个单元格都需要一个标头,只需将标头内容构建到该单元格中,而不是尝试将其放在单独的位置标头视图。 叹...

仍然有兴趣知道为什么viewForHeaderInSection似乎仅被调用一次。

添加此委托方法

- (CGFloat) tableView: (UITableView*) tableView heightForFooterInSection: (NSInteger) section

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM