Ketan Odedra
Ketan Odedra

Reputation: 1283

How to hide All section header in UITableViewcell (grouped style)?

I have two section in UITableviewCell. I want hide that two section. this is my code.

 -(CGFloat)tableView:(UITableView *)tableView  heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
    return CGFLOAT_MIN;
}
else
{
    return 32.0f;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [storename2 count];
}

 - (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
    return nil;
}
else
{
     return [storename2 objectAtIndex:section];
}
}

In Viewdidload.

- (void)viewDidLoad 
{
[super viewDidLoad];
 self->CartTableview.contentInset = UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0);
}

But its only Hide First Section, show below image i want like this (When Card Is Empty Hide Sections and when i add products to the card show all(two)section.) help me,

show 1st image. when i add product to card two section will show and when i remove the product all section will hidden. Thanks in Advance.

enter image description here enter image description here

Upvotes: 3

Views: 1778

Answers (4)

Krunal
Krunal

Reputation: 79656

Use switch-case to handle multiple section height.

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

    let headerHeight: CGFloat

    switch section {

    case 0:
        // first section for test - set value for height
        headerHeight = 0

    default:
        // other Sections - set value for height
        headerHeight = 0
    }

    return headerHeight
}

Upvotes: 0

Sathish Kumar VG
Sathish Kumar VG

Reputation: 2172

Just return 0 in heightForHeaderInSection tableview delegate method...

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    // Specific section
    if (section == 0)
            return 0.0f;

    // all sections
    return 0;
}

Upvotes: 1

user7587085
user7587085

Reputation:

Also don't forget to set UITableview Delegate & datasource.

- (void) viewDidLoad {
    [super viewDidLoad];
     self.tableView.contentInset = UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0);
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    // Specific section
    if (section == 0)
            return 0.0f;

    // all sections
    return 0;
}

- (NSString*) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section
{   // Specific section
    if (section == 0) {
        return nil;
    } else {
    // all sections
        // return some string here ...
    return nil;
    }
}

Upvotes: 1

jignesh Vadadoriya
jignesh Vadadoriya

Reputation: 3310

I think you need change in bellow method

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

And Remove this method

- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section
{
   if (section == 0)
   {
     return nil;
   }
   else
   {
     return [storename2 objectAtIndex:section];
   }
}

This will hide all the section header in your tableview.

Upvotes: 0

Related Questions