15

I want to set the title of the header in the section of UITableView. What is the syntax in swift to set the title of the header in the section.

func tableView( tableView : UITableView,  titleForHeaderInSection section: Int)->String
{
    switch(section)
    {
    case 2:
        return "Title 2"
        break
    default:
        return ""
        break
    }

}

func tableView (tableView:UITableView , heightForHeaderInSection section:Int)->Float
{

    var title = tableView.titleForHeaderInSection[section];
    if (title == "") {
        return 0.0;
    }
    return 20.0;
}

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

    var title = tableView.titleForHeaderInSection[section] as String
    if (title == "") {
        return UIView(frame:CGRectZero);
    }
    var headerView:UIView! = UIView (frame:CGRectMake(0, 0, self.tableView.frame.size.width, 20.0));
    headerView.backgroundColor = self.view.backgroundColor;

    return headerView;
}
buczek
  • 2,011
  • 7
  • 29
  • 40
Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63

4 Answers4

18

You can use the func already defined in your class i.e. :

self.tableView(tableView, titleForHeaderInSection: section)

For example, using your code:

func tableView( tableView : UITableView,  titleForHeaderInSection section: Int)->String {
   switch(section) {
     case 2:return "Title 2"

     default :return ""

   }
}

func tableView (tableView:UITableView , heightForHeaderInSection section:Int)->Float 
{

    var title = self.tableView(tableView, titleForHeaderInSection: section)
    if (title == "") {
        return 0.0
    }
    return 20.0
}
HungryArthur
  • 1,037
  • 11
  • 18
5

In order to call this method you must use the UITableViews titleForHeaderInSection method. This method will provide the index for the current section and you are required to return a String, the returned String will be set as the header.

In order to invoke this, lets assume we have an array of Strings called cars

cars = ["Muscle", "Sport", "Classic"]

We can then simply call

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? 
{
    // Ensure that this is a safe cast
    if let carsArray = cars as? [String]
    {
        return carsArray[section]
    }

    // This should never happen, but is a fail safe
    return "unknown"
}

This will return section titles in the order given above.

Halfpint
  • 3,967
  • 9
  • 50
  • 92
2

Update Swift 5

Xcode - 11.4

func tableView( _ tableView : UITableView,  titleForHeaderInSection section: Int)->String? {
   switch(section) {
     case 1:return "Title of section header"
     default :return ""
   }
}

This will show header in caps lock. To make it in regular mode as you want to show it use this method along with the above mentioned:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let titleView = view as! UITableViewHeaderFooterView
    titleView.textLabel?.text =  "Title of section header"//titleView.textLabel?.text?.lowercased()
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Harjot Singh
  • 6,767
  • 2
  • 38
  • 34
0
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0{
           return "Pass Your String For Header"
       }else{
          return "Pass Your String For Header"
     }
 }