1

I followed the instructions from this answer here and managed to get my custom UITableView header section like so:

enter image description here

override func viewDidLoad() {
    super.viewDidLoad()

    let nib = UINib(nibName: "TableSectionHeader", bundle: nil)
    billTableView.register(nib, forHeaderFooterViewReuseIdentifier: "TableSectionHeader")
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let cell = billTableView.dequeueReusableHeaderFooterView(withIdentifier: "TableSectionHeader")
    let header = cell as! TableSectionHeader
    header.lblPerson.text = array[section].name
    header.lblTotal.text = "$0.00"
    return cell
}

Everything works fine however I need a separator line for the sections and because the section is a UIView from my nib, I'm not able to use the .separatorStyle...

enter image description here

I need to add a separator line because I want to expand/collapse the rows. Much thanks for your help!

Community
  • 1
  • 1
iamhx
  • 472
  • 6
  • 24

1 Answers1

1

Maybe you need to add a seperator yourself like this :

CGRect seperatorFrame = CGRectMake(0, headerView.frame.size.height-1, tableView.bounds.size.width, 1);
UIView *seperatorView = [[UIView alloc] initWithFrame:seperatorFrame];
seperatorView.backgroundColor = [UIColor grayColor];
[headerView addSubview:seperatorView];

or use Autolayout in .xib:

xib autolayout

isaced
  • 1,735
  • 3
  • 16
  • 24
  • Hmm, it works, but only when I scroll it up and down, not when inserting sections. I put your code in the `viewForHeaderInSection` delegate. Where should I put this code so that when inserting sections the separator is also added? – iamhx Apr 22 '17 at 07:22
  • If you are using xib, you can try dragging a UIView(seperatorView) and configuring AutoLayout. – isaced Apr 22 '17 at 07:28
  • Yes, I am using xib. I'm not sure what it means by configuring autolayout.. Could you explain? Thanks! – iamhx Apr 22 '17 at 07:32
  • Ah, nvm. I got what you mean. I just added another UIView, changed its height and added constraints. With a little tweaking it looks perfect now :) Thanks! – iamhx Apr 22 '17 at 07:39
  • @iamhx Yes, that's what I mean – isaced Apr 22 '17 at 07:41