UITableview在标题部分上方添加空格

时间:2018-05-31 06:53:14

标签: ios objective-c uitableview

我目前在我的uitableview中有2个标题,

这是插图enter image description here

我想要实现的是在标题2和标题1的最后一个单元格之间加一个间距。

我目前的代码是:

-(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;
}

3 个答案:

答案 0 :(得分:2)

heightForFooterInSection检查部分并返回适当的值。并在UIView中返回透明的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;
}

为第一个标题添加顶部空间

-(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;
    }
}

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

试试这个   - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {               返回10.0f;          } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {           返回10.0f; }