OGeek|极客世界-中国程序员成长平台

标题: ios - 更改 HeaderInSection 的 UITableView 的高度? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:20
标题: ios - 更改 HeaderInSection 的 UITableView 的高度?

我的 UITableView 的样式很普通,我想更改 HeaderInSection 的高度,但它仍然是默认高度 22.0。

- (CGFloat)tableViewUITableView *)tableView heightForHeaderInSectionNSInteger)section
{
    if (tableView.tag == tableOrdersTag)
    {
        return 35;
    }
    else
    {
        return 0;
    }
}

- (UIView *)tableViewUITableView *)tableView viewForHeaderInSectionNSInteger)section
{
    if (tableView.tag == tableOrdersTag)
    {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frameWidth, 35)];

        view.backgroundColor = COMMON_BACKGROUND_COLOR;

        DDLogVerbose(@"=========tableView  %f", tableView.sectionHeaderHeight);

        return view;
    }
    else
    {
        return nil;
    }
}



Best Answer-推荐答案


你的 NSLog 的位置

        DDLogVerbose(@"=========tableView  %f", tableView.sectionHeaderHeight);

可能会导致问题。应该在返回方法之前:

        return view;

将数字 35 改为 100,你会发现它工作正常。

因为方法

tableView:viewForHeaderInSection 

落后于

tableView:heightForHeaderInSection.

当我创建一个没有 xib 或 Storyboard 的 TaleViewController 时:

- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView {
return 1;
}
- (CGFloat)tableViewUITableView *)tableView heightForHeaderInSectionNSInteger)section
{
    NSLog(@"heightForHeaderInSection");
        return 100;
}

- (UIView *)tableViewUITableView *)tableView viewForHeaderInSectionNSInteger)section
{

        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 100)];

        view.backgroundColor = [UIColor redColor];
        NSLog(@"=========tableView  %f", tableView.sectionHeaderHeight);
        return view;
}

它会像这样打印:

 2015-05-21 18:25:30.525 UITableViewHeaderDemo[24727:2350493] heightForHeaderInSection
 2015-05-21 18:25:30.526 UITableViewHeaderDemo[24727:2350493] heightForHeaderInSection
 2015-05-21 18:25:30.526 UITableViewHeaderDemo[24727:2350493] heightForHeaderInSection
 2015-05-21 18:25:30.526 UITableViewHeaderDemo[24727:2350493] heightForHeaderInSection
 2015-05-21 18:25:30.535 UITableViewHeaderDemo[24727:2350493] heightForHeaderInSection
 2015-05-21 18:25:30.535 UITableViewHeaderDemo[24727:2350493] heightForHeaderInSection
 2015-05-21 18:25:30.549 UITableViewHeaderDemo[24727:2350493] heightForHeaderInSection
 2015-05-21 18:25:30.549 UITableViewHeaderDemo[24727:2350493] heightForHeaderInSection
 2015-05-21 18:25:30.550 UITableViewHeaderDemo[24727:2350493] =========tableView  -1.000000

但是 It works fine

关于ios - 更改 HeaderInSection 的 UITableView 的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30370257/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4