Section header title lost when customize the section header view - ios

In my code I use below code to set the section header title of the tableview. And it works well.
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int)
-> String? {
//return self.adHeaders[section]
return self.headers[section]
}
I want to customize the background color of the header, so I insert below code before the above code.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor(red: 232/255.0, green: 237/255.0, blue: 242/255.0, alpha: 1.0)
return headerView
}
As a result, the background color changes, while the title text lost.

When you implement viewForHeaderInSection, titleForHeaderInSection isn’t called at all. So you need to set the title of your header view in viewForHeaderInSection. You probably need to create a UILabel.
You must also implement heightForHeaderInSection.

Related

How to display title for header text for header in first section iOS

Using method below of viewForHeaderInSection i try and display an image in table header section one , but i also want a heading text, using the method of heading title text i am able to display the heading for all sections but not section 1 , do i need to make some changes to heading view for section one to be able to display the heading , if i were to remove the viewForHeaderInSection then the title for first header section also shows up
let sectionTitle = ["Feedback", "Follow Us"]
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
headerTop.headerLogo.image = UIImage(named: "foodpin-logo")
return headerTop.topView
}
return nil
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitle[section]
}
You need to create a text label in your headerTop view and remove this function override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int). So your code will be like this:
let sectionTitle = ["Feedback", "Follow Us"]
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
headerTop.headerLogo.image = (section == 0) ? UIImage(named: "foodpin-logo") : nil
headerTop.yourHeaderTextLabel.text = sectionTitle[section]
return headerTop.topView
}
It seems like you are looking for tableHeaderView! The issue is you are overriding the first section's header.
You may consider setting the tableView's tableHeaderView for the headerTop instead. So all sections will have their titles as you expected.
tableView. tableHeaderView = headerTop
Also, if you want yout table's header to be sticky, there are bunch of tutorials for that.

How to show and hide Custom UITableView Header Cell of Table View in Swift3 iOS

I am developing an application in Swift3 where I have to show and hide UITableView Header for different users. For displaying UITableView Header View, I have created a custom class CustomHeaderCell of UITableViewCell.
Here is my code:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 235.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! CustomHeaderCell
return headerCell
}
Now Can anyone please help me to hide this Header of my UITableView?
Note: I tried using this tableView.tableHeaderView?.isHidden = true, but not working. Should I need to do the validation in heightForHeaderInSection?
Reference Link to Add HeaderViewCell: http://www.accella.net/knowledgebase/custom-header-and-footer-views-for-uitableviews/
If you have a way to differentiate users then you can just change the header height like this
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if userA {
return 235.0
} else {
return 0
}
}
That should help in hiding the header
You are mixing tableHeaderView and section header views, which are differents:
tableHeaderView is a view showed as a header for the whole UITableView
section header views are reusable views used for displaying header above each section
In your case, you want to use section header views, so you should return empty ones for non concerned users (I assume here sectionNeedHeader will be replaced by your condition).
Also, you better use UITableViewHeaderFooterView instead of UITableViewCell. The behavior is globally the same but it's made for this usage:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if sectionNeedHeader {
return 235.0
}
return 0.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if sectionNeedHeader {
let headerCell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderView") as! CustomHeaderView
return headerView
}
return nil
}

Removal of white line under section header in UITableView?

I'm struggling with removing the white lines below each custom section header in an UITableView, as seen below. Any suggestions? I already use this in my TableView.
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
The above solves only the separators between the cells, not for the headers.
The only thing I have in my custom section header is
containerCellView.backgroundColor = UIColor(red: 24/255.0, green: 34/255.0, blue: 41/255.0, alpha: 100)
Set heigth for header & footer to 0.01 it will solve your problem
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.01
}
Put this line in viewDidLoad():
self.tableView.separatorColor = [UIColor clearColor];
I found out that it was the backgroundColor of my header cell that caused the problem.
self.backgroundColor = UIColor.blackColor()
Solved the problem in the custom header class
Use this code were you set your header
headerView.layer.borderColor=[UIColor blackColor].CGColor;
headerView.layer.borderWidth=1.0f;
I was able to reproduce similar issue on iPhone 7 Plus/iOS 12.1. I have a class conforming to UITableViewDelegate and tableView(_:heightForHeaderInSection:) is implemented like that:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 100.0 // any float value
}
I added tableView(_:estimatedHeightForHeaderInSection:) to fix the problem:
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return 100.0 // any float value
}
All code in this post was tested in Xcode 10.2.1. I used Swift 5.

UITableView Section header color misbehaving

I changed the color of my custom UITableViewCell section header and while I'm scrolling inside my tableView it changes its alpha, I guess. Some are kind of transparent and the others have a solid color.
What is wrong?
// Customize HeaderView of Sections
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
// This changes the header background
view.tintColor = UIColor(hue: 0.13, saturation: 0.13, brightness: 0.13, alpha: 1)
// Gets the header view as a UITableViewHeaderFooterView and changes the text colour
var headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
headerView.textLabel.textColor = UIColor.whiteColor()
}
I would strongly suggest to configure your header view in this method:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
I would use the willDisplayHeader... for other purposes than primarily configuring the appearance.

Remove space between cell and header

I've create a uitableview with two sections. However there seems to be some unwanted space between the second section header and the last cell in the first section (see screenshot). Does anybody know how to remove it?
Code per request:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCellWithIdentifier("SectionHeader") as MyRentalsScetionsHeaders
switch(section){
case 0:
headerCell.sectionName.text = "Upcoming"
headerCell.backgroundColor = UIColor(red: 87/255.0, green: 189/255.0, blue: 135/255.0, alpha: 1)
default:
headerCell.sectionName.text = "Ended"
headerCell.backgroundColor = UIColor(red: 203/255.0, green: 205/255.0, blue: 200/255.0, alpha: 1)
}
return headerCell
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
switch(section){
case 0:
return 45.0
default:
return 30.0
}
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.00001
}
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView(frame: CGRectZero)
}
Edit
My current solution is setting the height for the footer to 0.00001, which is allowed. It's not really the most ideal solution, but it looks a lot better :) if anybody know a pixel-perfect solution, I'd love that!
Select TableView > Open Size Inspector > Set SectionHeight of Header to 0.
Select tableview and in attribute inspector. There is option to set space between header and footer. 22 is default.

Resources