laura-wen
laura-wen

Reputation: 45

Change the UITableView's height for HeaderInSection?

My UITableView's style is plain, and I want to change the height of HeaderInSection, but it's remains the default height 22.0.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView.tag == tableOrdersTag)
    {
        return 35;
    }
    else
    {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)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;
    }
}

Upvotes: 2

Views: 150

Answers (3)

ChenYilong
ChenYilong

Reputation: 8673

The place of your NSLog

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

may cause the problem.It should's before the return method:

        return view;

change the number 35 to 100, you'll find the it works fine.

because the method of

tableView:viewForHeaderInSection 

is behind of

tableView:heightForHeaderInSection.

When I create a TaleViewController without xib or storyboard like :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSLog(@"heightForHeaderInSection");
        return 100;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)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;
}

it will print like :

 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

but the It works fine

Upvotes: 1

Nirmit Dagly
Nirmit Dagly

Reputation: 1502

Do this things:-

  1. Open interface builder and select table view.
  2. Now, Open the size inspector.
  3. Find options to set section height.
  4. In that, you'll find Header and Footer. Set value of 35 to header.

Try out this. Hope this will solve your problem.

Upvotes: 1

MOHAMMAD ISHAQ
MOHAMMAD ISHAQ

Reputation: 998

First of all change section height from Xib then

Return following line instead of nil in else then check

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frameWidth, 0)];

Upvotes: 0

Related Questions