1

In a previous project using ObjectiveC, I had a multiple section tableView showing section headers with different background colours and with header text that dynamically updated itself dependent upon the number of items within the section. It worked perfectly. I have tried to replicate this code in a new project in which we are using Swift and it doesn't work. The header text displays correctly, but there is no background colour and most importantly, the section header overlays the top cell in each section rather than sitting above it. This is the relevant code:

    // MARK: TableView delegates

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    if let sections = fetchedResultsController!.sections {
        return sections.count
    }
    return 0
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if let sections = fetchedResultsController!.sections {
        let currentSection = sections[section]
        return currentSection.numberOfObjects
    }
    return 0
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView(frame: CGRectMake(0, 0, tableView.bounds.size.width, 30))
    let textHeader = UILabel(frame: CGRectMake(11, 0, 320, 20))
    switch (section) {
    case 0:
        headerView.backgroundColor = UIColor.greenColor()
    case 1:
        headerView.backgroundColor = UIColor.blueColor()
    case 2:
        headerView.backgroundColor = UIColor.redColor()
    case 3:
        headerView.backgroundColor = UIColor.purpleColor()
    default:
        break
    }
    let hText: String = "\(fetchedResultsController!.sections![section].name)"
    let hItems: String = "\((fetchedResultsController!.sections![section].numberOfObjects) - 1)"
    let headerText: String = "\(hText) - \(hItems)items"
    textHeader.text = headerText
    textHeader.textColor = UIColor.whiteColor()
    textHeader.backgroundColor = UIColor.clearColor()
    headerView.addSubview(textHeader)
    return headerView
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat? {
    return 20.0
}
Hasya
  • 9,792
  • 4
  • 31
  • 46
  • please add a relevant screenshot using imgur.com site – Shubhank Apr 29 '16 at 09:10
  • Sorry, I no longer have access to that project, but it was just a full width header, 20 high, that had different colours and looked like a divider between the sections. it sat above the 1st cell in each section and never overlaid it as is happening in this new Swift code. – Rockhopper9 Apr 29 '16 at 17:40

2 Answers2

3

The header overlays because its height is 30, and you return only 20.0 in heightForHeaderInSection func. the background color doesn't show up because you miss a break for each "case" in the switch statements.

MudOnTire
  • 506
  • 3
  • 9
0

Switch cases should end with break, then only it will return the backgroundcolor

Check below code

switch (section) {
case 0:
    headerView.backgroundColor = UIColor.greenColor()
    break
case 1:
    headerView.backgroundColor = UIColor.blueColor()
    break
case 2:
    headerView.backgroundColor = UIColor.redColor()
    break
case 3:
    headerView.backgroundColor = UIColor.purpleColor()
    break
default:
    break
}
Sheereen S
  • 1,292
  • 10
  • 18
  • 1
    Whilst this was correct with Objective C, in Swift you no longer include the break statement in each case, unless you want to execute no statements for a particular case. – Rockhopper9 Apr 29 '16 at 17:35