2

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;

}
Code cracker
  • 3,105
  • 6
  • 37
  • 67
Raj
  • 67
  • 2
  • 9
  • posiable duplicate [this](http://stackoverflow.com/questions/9737616/uitableview-hide-header-from-empty-section) . please search before you ask something – Huy Nghia Mar 04 '15 at 05:29
  • How many sections do you have in your table? – Abhishek Nath Mar 04 '15 at 05:33
  • @AbhishekNath 3 section bro – Raj Mar 04 '15 at 05:37
  • @huy nghia i have tried this link its not helped me – Raj Mar 04 '15 at 05:38
  • [reverseOrder1 objectAtIndex:section] is NSString ? you want to remove header when header title nil? try set viewForHeaderInSection: return nil heightForHeaderInSection: = 0 when [reverseOrder1 objectAtIndex:section] nil – Huy Nghia Mar 04 '15 at 05:49
  • I find I have similar problem. Except I've set my header view to nil AND height to 0 for empty sections. Still doesn't work somehow. – Flying_Banana Mar 07 '15 at 22:22

2 Answers2

2

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;
     }
    }
Ravi Gautam
  • 960
  • 2
  • 9
  • 20
0

In addition to Ravi's answer, make sure you set the section height property of table view to 0 in storyboard.

Flying_Banana
  • 2,864
  • 2
  • 22
  • 38