Anuradh S Anuradh S - 3 months ago 9
Objective-C Question

why view for header in section does not appear properly when the tableview style is grouped in iOS, objective c

I'm working with a tableview which has four sections. for each section I have implemented a header view. when I use the

tableview
style
as
Plain
it works properly. but if I use the
tableview
style
as
Grouped
it looks wired.

this is how I implement the
tableview
delegate methods.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return 1;
}
else if(section == 1)
{
return 1;
}
else if (section == 2)
{
return 3;
}
else
{
return 1;
}
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, 50)];
UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, screenSize.width-80, 50)];

if (section == 0)
{
titlelabel.text = @"Flight Summary";
[headerView addSubview:titlelabel];
return headerView;
}
else if(section == 1)
{
titlelabel.text = @"Price Summary";
[headerView addSubview:titlelabel];
return headerView;
}
else if (section == 2)
{
titlelabel.text = @"Traveller Details";
[headerView addSubview:titlelabel];
return headerView;
}
else
{
titlelabel.text = @"Book Now";
[headerView addSubview:titlelabel];
return headerView;
}
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"checkcell"];
return cell;
}


this is the screenshot when I use tableview style as
Plain


enter image description here

link to image : image with plain style

this is the screenshot when I use tableview style as
Grouped


enter image description here

link to the image : image with grouped stye

I have no idea what is going on, hope your help with this.

Answer

It requires to set the section header height. Try the below way, Hope this helps you.

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