Remove space between cell and header - ios

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.

Related

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
}

Section header title lost when customize the section header view

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.

Set margin on top of a tableview in IOS

I'm new to IOS development with swift and I'm having a problem. I need to create a tableview and it looks almost the way I wanted, except for the space at the top of the first section of the table. It has no name but I would like to reduce the space between the top and the first item. What I was able to do is according to the code and image below:
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch (section) {
case 0:
return ""
default:
return self.nameSection2
}
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.white
let headerLabel = UILabel(frame: CGRect(x: 15, y: 8, width:
tableView.bounds.size.width, height: tableView.bounds.size.height))
headerLabel.font = UIFont(name: "Verdana", size: 16)
headerLabel.textColor = UIColor.lightGray
headerLabel.text = self.tableView(self.tableView, titleForHeaderInSection: section)
headerLabel.sizeToFit()
headerView.addSubview(headerLabel)
return headerView
}
The 'margin' you see is because the height for both the section headers is the same. The second one looks less-empty as it actually has a title.
You can modify the height for the headers to reduce the space:
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
switch section {
case 0:
return 0
case 1:
return 44 //Required height value here
default:
return defaultValue //Any default value
}
}
You need to implement the heightForHeaderInSection so you can collapse that header. See below:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 1.0
} else {
return 32.0
}
}
You can set the appropriate value for the else condition for your needs but this gives you the idea.
Update #1: I found this link in searching that may help as well: http://stackoverflow.com/a/23955420/3965
It recommends using GLFloat's minimum value instead:
if section == 0 {
return CGFloat.leastNormalMagnitude
}
return tableView.sectionHeaderHeight
Implement heightForHeaderInSection and return the height you want for the first section.
Also, you wouldn't normally implement titleForHeaderInSection and viewForHeaderInSection. Just put your switch statement in viewForHeaderInSection to set the text for your label.
And you don't need to put your UILabel into headerView, just return the label. Or instead of UIView, use UITableViewHeaderFooterView.

Too long text for titleForHeaderInSection

So what my problem is that my text above my section is too long and gets cut off.
Any way to solve this like making it two rows long?
Any help is appreciated
You need to define the heightForHeaderInSection and customize viewForHeaderInSection. You can either fix all header heights at a value big enough for all lines, or calculate the required height for the specific header (as below).
let headerFont:UIFont = UIFont.systemFontOfSize(14);
let headerTexts = ["one line", "two line test123 sadfjklsadf asdjfklasjdflk asdfjklasdjfl asdfjklsadf"];
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2;
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return heightOfHeaderText(headerTexts[section]);
}
func heightOfHeaderText(text:String) -> CGFloat{
return NSString(string: text).boundingRectWithSize(
CGSizeMake(self.tableView.frame.size.width, 999),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName : headerFont],
context: nil).size.height;
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerLabel:UILabel = UILabel.init(frame: CGRectMake(0, 0, tableView.frame.size.width, self.tableView(tableView, heightForHeaderInSection: section)));
headerLabel.numberOfLines = 0;
headerLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping;
headerLabel.font = headerFont;
headerLabel.text = headerTexts[section];
return headerLabel;
}
Make a custom view with a label in it. And use viewForHeaderInSection delegate method to assign text to that label and return this view.
EDIT:
See this link
Customize UITableView header section

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.

Resources