How to hide header if string/object is null in UITableView - ios

I want to hide header and its space if if string/object is null in UITableView. Here is my coding. In this code, if object is null, header space remains there, but it shouldn't.
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CompanyObj *compObj=[self.arrCompList objectAtIndex:section];
NSString *compname;
if ([compObj.compName isEqualToString:#""]) {
[self NochangeHeight];
return nil;
} else {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 36)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, tableView.frame.size.width, 36)];
if ([compObj.clientList count] != 0) {
compname = compObj.compName;
[label setFont:[UIFont fontWithName:#"AvenirNextLTPro-Regular" size:25.0f]];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentLeft;
[label setText:compname];
label.backgroundColor = [UIColor orangeColor];
[view setBackgroundColor:[UIColor orangeColor]];
[view addSubview:label];
[self changeHeight];
}
return view;
}
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return headerHeight;
}
- (void)changeHeight {
[_clientListGroupTableView beginUpdates];
headerHeight = 50.0;
[_clientListGroupTableView endUpdates];
}
- (void)NochangeHeight {
[_clientListGroupTableView beginUpdates];
headerHeight = 0.0;
[_clientListGroupTableView endUpdates];
}

You have to return a different value in the heightForHeaderInSection: method instead.
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CompanyObj *compObj=[self.arrCompList objectAtIndex:section];
if ([compObj.compName isEqualToString:#""]) {
return 0;
} else {
return headerHeight;
}
}

You can make the height 0 if it is "", rather than controlling in viewForHeaderInSection.
The method viewForHeaderInSection arrange the view of header according to what you are returning. But it does arrange on to the existing header. So header is always there, and you are just making labels, texts etc. onto that header. As a consequence, when you say it to be "nil", since it is just what should be on the header, programme will understand it as "There is nothing to put on to the header". Thus, header will just be an empty space.
In order not to see header, you should change the height, within the method heightForHeaderInSection. So if you implement also that method as:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CompanyObj *compObj=[self.arrCompList objectAtIndex:section];
if ([compObj.compName isEqualToString:#""]) {
return 0;
} else {
return headerHeight;
}
}

Related

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
link to image : image with plain style
this is the screenshot when I use tableview style as Grouped
link to the image : image with grouped stye
I have no idea what is going on, hope your help with this.
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
}

how to remove the empty title headers

I am having a table view, in that I am having title headers, they are working well, but the sum of the title header values are getting null, I want to hide that null title header in my table view. how can I do this process. please help me in coding.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [reverseOrder1 objectAtIndex:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 75)];
UILabel *headerTitile =[[UILabel alloc]initWithFrame:CGRectMake(20, -5, tableView.bounds.size.width-20, 75)];
headerTitile.text = [reverseOrder1 objectAtIndex:section];
headerTitile.textColor = [UIColor whiteColor];
headerTitile.TextAlignment=NSTextAlignmentCenter;
[headerView addSubview:headerTitile];
headerTitile.font = [UIFont fontWithName:#"Arial" size:16.0f];
headerView.backgroundColor = [UIColor colorWithRed:48/255.0f green:119/255.0f blue:21/255.0f alpha:1];
return headerView;
}
I am getting like above image,i ant to hide the null value title headers.
Make changes in heightForHeaderInSection is return 0 if no objects
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//Note use your main array which defines section's row and according to each section it may be dictionary or array use accordingly
NSArray *arrObjects = yourSectionsRowData[section];
if([arrObjects count]>0])
return 60.0;
else
return 0.0;
}
Also return view nil if not found any objects in viewForHeaderInSection
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//Note use your main array which defines section's row and according to each section it may be dictionary or array use accordingly
NSArray *arrObjects = yourSectionsRowData[section];
if([arrObjects count]>0])
return nil;
else
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 75)];
UILabel *headerTitile =[[UILabel alloc]initWithFrame:CGRectMake(20, -5, tableView.bounds.size.width-20, 75)];
headerTitile.text = [reverseOrder1 objectAtIndex:section];
headerTitile.textColor = [UIColor whiteColor];
headerTitile.TextAlignment=NSTextAlignmentCenter;
[headerView addSubview:headerTitile];
headerTitile.font = [UIFont fontWithName:#"Arial" size:16.0f];
headerView.backgroundColor = [UIColor colorWithRed:48/255.0f green:119/255.0f blue:21/255.0f alpha:1];
return headerView;
}
}

How to remove the empty section header in UITableView

I am having a UITableView in that I am having three title headers.problem is: when my title header value become empty means that header should not show in the table view. I have tried many method its not helped for me. can anyone help me please.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [reverseOrder1 objectAtIndex:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 75)];
UILabel *headerTitile =[[UILabel alloc]initWithFrame:CGRectMake(20, -5, tableView.bounds.size.width-20, 75)];
headerTitile.text = [reverseOrder1 objectAtIndex:section];
headerTitile.textColor = [UIColor whiteColor];
headerTitile.TextAlignment=NSTextAlignmentCenter;
[headerView addSubview:headerTitile];
headerTitile.font = [UIFont fontWithName:#"Arial" size:16.0f];
headerView.backgroundColor = [UIColor colorWithRed:48/255.0f green:119/255.0f blue:21/255.0f alpha:1];
return headerView;
}
You can use like this
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section == 0])
{
return 0;
}
else
{
return 60;
}
}
In addition to Ravi's answer, make sure you set the section height property of table view to 0 in storyboard.

ViewForHeaderInSection code works only on header dequeueing

I have custom tableView header, but the code where I specify header text color and separator line works only when the headers are dequeued, when I scroll them off the screen. Only then they return with white text color and visible separator. What is wrong with my code that the headers do not return in the correct state right on the first load?
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return #"FIRST HEADER";
} else if (section == 1) {
return #"SECOND HEADER";
} else {
return #"THIRD HEADER";
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UITableViewHeaderFooterView *myHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:#"header"];
if (!myHeaderView) {
myHeaderView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:#"header"];
}
myHeaderView.textLabel.textColor = [UIColor whiteColor];
CGFloat leftMargin = 0.0;
CGFloat lineWidth = 1.0;
UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(leftMargin, myHeaderView.frame.size.height - lineWidth, myHeaderView.frame.size.width - leftMargin, lineWidth)];
separatorLine.backgroundColor = [UIColor whiteColor];
[myHeaderView addSubview:separatorLine];
return myHeaderView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20.0;
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ([view isMemberOfClass:[UITableViewHeaderFooterView class]]) {
((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = [UIColor clearColor];
}
}
The reason is that your header's frame is equal to CGRectZero before dequeueing. It means, that header will be resized in future and you need to set appropriate autoresizing mask to separator. Then, separator will also be resized.
UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(leftMargin, 0, 0, lineWidth)];
separatorLine.backgroundColor = [UIColor redColor];
separatorLine.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[header.contentView addSubview:separatorLine];

Setting header for only one uitableview

I need help. I have three UITableViews in one UIView. However, I only want one UITableView to have a header. How can I say it in code? I already have the following code. Thanks guys.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (tableView == Table1) {
return #"My Header Text";
} else if (tableView == Table2) {
return nil;
} else if (tableView == Table3) {
return nil;
}
}
The code you provided will be add a header view for a certain section only. So If you have two or more section then you need to write extra logic to set the header view for each section.
Here it is an easy way to set the tableHeaderView for single table is.
UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
label.text = #"my table header";
table1.tableHeaderView = label;
If that's not working you could also try to return zeroes for header's heights as well:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (tableView == Table1)
{
return 50.0; // or what's the height?
}
else
{
return 0.0;
}
}
Try this
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(20, 6, 300, 30);
label.textColor = [UIColor blackColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
[view addSubview:label];
return view;
}

Resources