Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we add image in UITableView section header using swift?

In my willDisplayHeaderView I have changed color of section header But I want to add an image before section title. Any help? My code for willDisplayHeaderView is

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView 
    header.contentView.backgroundColor = UIColor(red: 238/255, green: 168/255, blue: 15/255, alpha: 0.8)
    header.textLabel.textColor = UIColor.whiteColor()
    header.alpha = 1
}
like image 399
Saqib Omer Avatar asked Feb 19 '15 07:02

Saqib Omer


People also ask

How to add header and footer in tableView in Swift?

To create a basic header or footer with a text label, override the tableView(_:titleForHeaderInSection:) or tableView(_:titleForFooterInSection:) method of your table's data source object. The table view creates a standard header or footer for you and inserts it into the table at the specified location.

What is UITableView in Swift?

A view that presents data using rows in a single column.

What is header in Swift?

For output message, the basic header identifies the application through which SWIFT has processed the message. The basic header also identifies the type of output data, the receiving logical terminal, and (if required) the output session number and output sequence number.


1 Answers

Swift 5

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let imageView = UIImageView();
    let image = UIImage(named: "logo")
    imageView.contentMode = .center
    imageView.image = image;
    return imageView
}

  // Optionally you can set the header height here.
  override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 85
    }
like image 152
MrAn3 Avatar answered Sep 23 '22 18:09

MrAn3