2

I've removed the separator left margin from my tableview using in the viewDidLoad:

self.tableView.layoutMargins = UIEdgeInsetsZero
self.tableView.separatorInset = UIEdgeInsetsZero

In my cellForRowAtIndexPath:

cell.layoutMargins = UIEdgeInsetsZero

The problem is that the header and footer views has been moved too and I want to keep it aligned with the cells' content.

enter image description here

I've tried to change the frame in the willDisplayHeaderView and willDisplayFooterView without success. Any suggestion? Thanks!

Fran Fox
  • 501
  • 5
  • 15

3 Answers3

2

As Memon Irshad suggest, I've created a new label but rather than do it inside the viewForHeaderInSection I've do it in willDisplayHeaderView because while using this method we can know the size of the headerView.

Then I hide the original textLabel of the TableViewHeader and adding the new framed label. What do you think? Any improvements?

Looks like this:

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
    let originView = view as! UITableViewHeaderFooterView

    let lbl = UILabel(frame: CGRectMake(15,originView.frame.height-30,originView.frame.size.width,20))

    // Setting up the new label
    lbl.numberOfLines = 0
    lbl.font  = Constants.Fonts.HeaderRow
    lbl.textColor = Constants.Colors.TextColored

    // Copying the original text content
    lbl.text = originView.textLabel?.text

    // Hidding the original label
    originView.textLabel?.hidden = true

    originView.addSubview(lbl)
}
Fran Fox
  • 501
  • 5
  • 15
  • `originView.textLabel?.hidden = true` was very important, otherwise the labels would just overlap each other and look weird. – Anjan Biswas Nov 08 '17 at 06:15
  • adding a label here is a bad idea, bc the delegate method gets called multiple times and each time you're adding a new label above already existing one when the view is reused. – Nik Yekimov Mar 14 '18 at 01:07
1

Header/Footer cannot stick to your cell's content. I too wasted a lot of time on that

You must add a row to your section and use it as if it is a header/footer

Akshansh Thakur
  • 5,163
  • 2
  • 21
  • 38
  • Thanks. Then Can I preserve the design? (Header without separator lines) – Fran Fox Jun 10 '16 at 10:28
  • I'm sorry, but what separator lines are you talking about? – Akshansh Thakur Jun 10 '16 at 10:30
  • You can customize that particular row(the one you want to use as header) as much as you want, you can change its height/you can change color, borderwidth everything. It can be customized easily. You can use the indexPath.section and access the row number. then you can customize that cell – Akshansh Thakur Jun 10 '16 at 10:35
  • Thanks Akshansh, I'm trying another solution. I'll post the reply if I solve the problem ;) – Fran Fox Jun 10 '16 at 13:15
1

try this code set custom label frame in header view

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

    let view = UIView()
    let lbl = UILabel(frame: CGRectMake(15,2,self.view.frame.size.width,20))
    lbl.textColor = UIColor.whiteColor()
    lbl.text = "SOPORTE"
    lbl.font = UIFont(name: "Helvetica Neue", size: 15)
    view.addSubview(lbl)

    return view
}
Memon Irshad
  • 972
  • 1
  • 8
  • 17