KikX
KikX

Reputation: 217

UITableview add space above the header section

I Currently have 2 header in my uitableview,

this is the illustrationenter image description here

What i want to achieve is to put a spacing between my header 2 and the last cell of header 1.

My current code is this:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    //filtered list for usd //Flawrence 5/30/18
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 45)];
    view.backgroundColor = [UIColor redColor];
   view.layer.borderColor = [UIColor blackColor].CGColor;
   view.layer.borderWidth = .5;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    button.transform = CGAffineTransformMakeScale(-1.0, 1.0);
    button.titleLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);
    button.imageView.transform = CGAffineTransformMakeScale(5.0, 1.0);
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 24);


    button.frame = view.frame;
  //  button.titleLabel.textAlignment = NSTextAlignmentCenter;
    button.titleLabel.tintColor = [UIColor blackColor];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.textColor  = [UIColor blackColor];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [view addSubview:button];

    if (section == 0) {

        [button setTitle:@"HEADER 1" forState:UIControlStateNormal];

    }
    else {


            [button setTitle:@"HEADER 2" forState:UIControlStateNormal];


    }
    return view;
}

Upvotes: 0

Views: 1216

Answers (3)

RajeshKumar R
RajeshKumar R

Reputation: 15748

In heightForFooterInSection check section and return appropriate value. And return transparent UIView in viewForFooterInSection.

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
       return 10;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footer = [[UIView alloc] init];
    [footer setBackgroundColor:[UIColor clearColor]];
    return footer;
}

To add top space for first header

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    //setup header view
    if (section == 0) {
        view.frame = CGRectMake(0, 0, tableView.frame.size.width, 55);
        button.frame = CGRectMake(0, 10, tableView.frame.size.width, 45);
    } else {
        view.frame = CGRectMake(0, 0, tableView.frame.size.width, 45);
        button.frame = CGRectMake(0, 0, tableView.frame.size.width, 45);
    }
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 55;
    } else {
        return 45;
    }
}

Upvotes: 2

Jigar
Jigar

Reputation: 1821

try this

 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
              return 10.0f;
         }

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

Upvotes: 0

Bhaumik Surani
Bhaumik Surani

Reputation: 1833

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 20.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

Upvotes: 0

Related Questions