How to set background colour for header in TableView? - swift

I found similar questions, but none of them helped me.
I need to set the background colour of headers in my table to white, it is gray now(same as background colour of the table).
Here are my tableView functions:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
let header = view as! UITableViewHeaderFooterView
header.backgroundColor = UIColor.white
header.textLabel?.font = UIFont(name: "Futura", size: 13)!
header.textLabel?.textColor = UIColor.blue
}
func tableView(tableView: UITableView, ViewForHeaderInSection section: Int) -> UIView? {
tableView.backgroundColor = UIColor.white
return tableView
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
return titles[section]
}
I'm setting background colour to white, but nothing changes.

Swift 3 - What did the trick for me was setting the header's contentView's backgroundColor.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header")
header.contentView.backgroundColor = .blueSteel
return header
}
Hope that helps someone.

Use viewForHeaderInSection method of UItableview and Create UIview
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.white
return headerView
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 15
}

In Swift 3, this worked for me:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let headerView = view as! UITableViewHeaderFooterView
headerView.textLabel?.font = UIFont(name: "Futura", size: 13)!
headerView.backgroundView?.backgroundColor = .white
}

Swift 3 below. One notable difference to previous answer, don't forget the override keyword. If you do, it won't override the function and will not work.
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.darkGray
return headerView
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 15
}

Related

Multiple headerview in tableview in swift

I want to create 3 headers in the tableview given in the images and 8 students in each row, my code is as follows
Screenshot of the example
class ViewController: UIViewController {
var schoolOfList: [Any] = [
[["school": ["VIP", "Bright International", "Motherland"]],
[["student1": ["Chirag", "Akshay", "Shailesh", "Vishal", "Mehul", "Vinod", "Mukesh", "Darshan"]],
["student2": ["Manjari", "Kairav", "Abhimanyu", "Akshara", "Arohi", "Neel", "Naksh", "Naman"]],
["student3": ["Paritosh", "Anuj", "Kavya", "Samar", "Vanraj", "Kinjal", "Anupama", "Riyan"]]]]]
#IBOutlet weak var mytableview : UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ViewController:UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> (Int) {
return schoolOfList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath)
cell.textLabel?.text = schoolOfList["school"] as! [String]indexPath.row
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerview = UIView(frame: CGRect(x:0,y:0,width:self.mytableview.frame.width,height:50))
headerview.backgroundColor = .green
let lable = UILabel(frame: CGRect(x:0,y:0,width:self.mytableview.frame.width,height:50))
lable.textAlignment = .center
lable.text = (schoolOfList["school"]as!String)
headerview.addSubview(lable)
return headerview
}

Re-usable UITableView "base class" with custom section header

I have a custom table view section header (set of colored lines) that I am using in around a dozen different table views. Currently, I'm cutting and pasting the same code in every single UITableViewDelegate handler.
For example, something like this:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
let lineView = UIView(frame: CGRect(x: 40, y: 0, width: tableView.frame.width - 80, height: 1))
lineView.backgroundColor = .separator
headerView.addSubview(lineView)
return headerView
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 1
}
Is it possible to subclass or extend UITableView in some way so that this code is written once and automatically applied to any of my specified tableviews?
Create a class BaseVC and conform UITableViewDelegate protocol. Implement viewForHeaderInSection and heightForHeaderInSection methods in it, i.e.
class BaseVC: UIViewController, UITableViewDelegate {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
let lineView = UIView(frame: CGRect(x: 40, y: 0, width: tableView.frame.width - 80, height: 1))
lineView.backgroundColor = .separator
headerView.addSubview(lineView)
return headerView
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 1
}
}
Now, inherit your ViewControllers from BaseVC like so,
class VC: BaseVC, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
return cell
}
}

How do I customize the header background color, height, and text of a UITableView?

I want to customize my section headers for a UITableView. How do I add text color, background color, and section header height?
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
view.tintColor = UIColor.black
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50.0
}

How to display data correctly in spacing table view cells

Only the first element from the array is displayed in my spacing cells.
I have no problems displaying data in simple cells without spacing, but when I need spacing cells and change my code, only the first item is displayed
let test = ["1","2","3"]
func numberOfSections(in tableView: UITableView) -> Int {
return test.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
let cellSpacingHeight: CGFloat = 10
return cellSpacingHeight
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.clear
return headerView
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SellerPlacesCell", for: indexPath) as! SellerPlacesCell
cell.backgroundColor = UIColor.white.withAlphaComponent(0.5)
cell.namePlace.text = test[indexPath.row]
return cell
}
How to fix it?
if in
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return 1
}
return 1
i replace return 1 to test.count have this:
Replace
cell.namePlace.text = test[indexPath.row]
with
cell.namePlace.text = test[indexPath.section]
the spacing is for the fact that you display sections , so you need to access the array with section not row

Swift 2.2 UITableViewHeaderFooterView transparent

I'm trying to set a transparent background to a TableViewHeader but without success.
First of all, would like to know if it's possible?
This is what I've done
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.contentView.backgroundColor = UIColor.clearColor()
}
My final objective is to add under my uitableview an UIView that'll host a GMSMapView and have a transparent HeaderView with a size of 100px.
Any help?
Thanks
You need to do following steps
1) in storyboard Add headerfooterview to tableview
2)In view did load write below code
tableView.registerClass(HeaderView.self, forHeaderFooterViewReuseIdentifier: "HeaderView")
headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier("headerView") as? HeaderView
3) add tableview delegate methods
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 110;
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
headerView.backgroundColor = UIColor.clearColor()
return headerView;
}
Hope this will help you
Steps to follow:
1) Drag and drop a UIView from object library to the top of the tableview to make a tableview header.
2)make a IBOutlet of the view in the Tableview controller.
3)lastly call this UITableview delegate Method
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
yourHeaderView.backgroundColor = UIColor.clearColor()
return yourHeaderView
}
// this is called when you need to make a transparent footer view
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
footerView.backgroundColor = UIColor.clearColor()
return footerView
}
//for transparent your table view section header:
//In Swift 5
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = UIColor.clear
}