Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customise header section in static cell?

I want to customise the header section in my application but it's in static cell. I tried to make one cell to be my header by including identifier and add a new file to control that cell but that doesn't work. I tried to drag an object into new file but it's can't be done. So how to customise section header? Is my way of approach is good?

like image 828
asiandudeCom Avatar asked Jul 28 '15 15:07

asiandudeCom


2 Answers

There are a couple of ways to customize the header section in a UITableView. For instance, if all you want to do is change the text, you can do so in the attributes inspector while making sure your TableViewSection is selected:

enter image description here

enter image description here

However, if you want the ability to do customizations such as text size, tont, capitalizations - any customizations inherent to UILabels, you'll need to override this method from the UITableViewController:

  override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as! UITableViewHeaderFooterView
    header.textLabel.textColor = UIColor(red: 243/255, green: 153/255, blue: 193/255, alpha: 1.0)
    header.textLabel.font = UIFont(name: "Helvetica Neue", size: 18)
    header.textLabel.text = "About Us"
    header.textLabel.frame = header.frame
    header.textLabel.textAlignment = NSTextAlignment.Left
  }

For example, in the code above, I took the header that was passed as a parameter and configured the textColor, font, text, alignment - really anything you can do on the UILabel can be done here.

Before customization

enter image description here

After customization

enter image description here

like image 134
Kelvin Lau Avatar answered Nov 16 '22 11:11

Kelvin Lau


Also you can customise header section use Nib. Simple example below.

HeaderView.xib (Screenshot)

HeaderView.swift

import UIKit

class HeaderView: UITableViewHeaderFooterView {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var titleBackgroundView: UIView!

     static var nib: UINib {
       return UINib(nibName: identifier, bundle: nil)
     }

     static var identifier: String {
       return String(describing: self)
     }

    override func awakeFromNib() {
      titleBackgroundView.layer.shadowColor = UIColor.gray.cgColor
      titleBackgroundView.layer.shadowOpacity = 0.5
      titleBackgroundView.layer.shadowOffset = CGSize(width: 0, height: 5)
      titleBackgroundView.layer.shadowRadius = 5
    }
}

ProfileTableViewController.swift

import UIKit

class ProfileTableViewController: UITableViewController {

    override func viewDidLoad() {
      super.viewDidLoad()
        tableView.register(HeaderView.nib, forHeaderFooterViewReuseIdentifier: HeaderView.identifier)
    }


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

      let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: HeaderView.identifier) as! HeaderView
        switch section {
        case 0:
          headerView.titleLabel.text = "Profile"
          return headerView
        case 1:
          headerView.titleLabel.text = "Social"
          return headerView
        default:
          return UIView()
    }

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
}

Simulator (Screenshot)

like image 38
Alexander Kandalov Avatar answered Nov 16 '22 09:11

Alexander Kandalov