Removal of white line under section header in UITableView? - ios

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.

Related

How to resize and change color of headlines in tableview xcode

I am using a static Tableview on my storyboard
that has headlines to the sections
The headlines text color and size is a static thing and I cannot change it
it results in very narrow headlines and black text.
how can I space out the headlines (make the height a bit bigger) and change the color of the text?
how can i space out the headlines (make the height a bit bigger) and change the color of the text ?
You'll need to have a custom view that has a label, and return it to the delegate method of the UITableView's viewForHeaderInSection.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
Ref:
https://www.hackingwithswift.com/example-code/uikit/how-to-add-a-section-header-to-a-table-view
https://developer.apple.com/documentation/uikit/uitableview/1614965-headerview
EDIT:
Here's how to achieve this. Basically you'll need a custom view in the delegate method I mentioned above. If you've successfully made a custom UITableViewCell in your cellForRow before, then this one should be a piece of cake for you.
You declare a container view, and then add your subviews in that container, in your case, a UILabel. I always use constraints, like so:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// Let's make the even numbers sections a red background.
// Blue background for odd numbers
let container = UIView()
container.backgroundColor = section % 2 == 0 ? .red : .blue
let titleForHeaderLabel = UILabel()
titleForHeaderLabel.backgroundColor = .white
titleForHeaderLabel.text = "HEADER SECTION: \(section)"
container.addSubview(titleForHeaderLabel)
titleForHeaderLabel.translatesAutoresizingMaskIntoConstraints = false
titleForHeaderLabel.topAnchor.constraint(equalTo: container.topAnchor, constant: 20).isActive = true
titleForHeaderLabel.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: -20.0).isActive = true
titleForHeaderLabel.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 20.0).isActive = true
titleForHeaderLabel.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -20.0).isActive = true
return container
}
Then you provide a height for your section in the func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat delegate method, like so:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 80.0
}
Output:
Easy, right? :) I hope this helps!

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.

Autolayout broken in Xcode-9 beta

I am having UITableView in my one of screens. It shows section headers and rows with dynamic height.
There are two issues with this set-up when I moved from Xcode8.3 to Xcode-9 beta.
1 Header view height broken
2 Dynamic height of rows broken
I have set-up my table view as:
Then I have custom cell
Here is code:
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return //dynamically calculated height according to msg text
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerHeight:CGFloat = tableView.sectionHeaderHeight
let headerView = HeaderView(frame: CGRect(x: 0, y: 0, width: tableView.width, height: headerHeight), withHeaderLable: "Date/Day of msg")
return headerView
}
This works perfect in Xcode8.3 even with multiple lines of msgs, but broken in Xcode9 beta as:
When I run with Swift3.2 settings in Xcode9 beta:
When I run with Swift4 settings in Xcode9 beta:
What would be the reason of this behavior?
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 30.0
Add this in your viewDidLoad and no need of tableview delegate method estimatedHeightForRowAt.

How to remove iOS TableView Separator

Hello, thanks for reading.
I 've tried to remove these separators since a while, it's horrible I can't delete them. I have tried a lot of answers in stackoverflow but no one was helpfull :/
Here is the problem :
I can't remove these white spaces between cells :s.
I've tried :
Change background color to same grey than cell,but it does not works
Checking cell size
Set separatorStyle to UITableViewCellSeparatorStyle.None
Returning the good height with tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
Disabling separator in main storyboard
... I really don't understand how to remove these white space ...
Sorry for my english,
Thanks
I'm adding this. Now you can check my settings : (And you can see separators are disabled)
you just need to change this in the Attribute inspector:
Try this tableView method:
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
{
return 0.00001
}
Try this hope it will be help full to you.
Objective C :
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
return 0.1f;
}
-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
return [[UIView alloc] initWithFrame:CGRectZero] ;
}
Swift :
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView(frame: CGRectZero)
}
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.1f
}
set the tableviewcell separator none likewise the image http://imgur.com/MbVA7pM
Try this out:
func tableView(tableView: UITableView!, willDisplayCell cell: UITableViewCell!, forRowAtIndexPath indexPath: NSIndexPath!) {
// Remove seperator inset
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset = UIEdgeInsetsZero
}
// Prevent the cell from inheriting the Table View's margin settings
if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
cell.preservesSuperviewLayoutMargins = false
}
// Explictly set your cell's layout margins
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins = UIEdgeInsetsZero
}
}
In swift hiding the table view separator
func tableView(tableView: UITableView!, willDisplayCell cell: UITableViewCell!, forRowAtIndexPath indexPath: NSIndexPath!)
{
cell.backgroundColor = UIColor.clearColor()
}
Ok I'm sorry, the problem was on my code, so you could not answer me ...
It was in my function : tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
Somewhere I wanted a border on my cell, so I have write:
var layer: CALayer = cell.layer
layer.borderColor = UIColor.rgbaColor(red: 288, green: 83, blue: 88, alpha: 1).CGColor
layer.borderWidth = 2
But this code didn't work because red:288 should be red:188, so I had a white border instead of an error, and I did not noticed ... Sorry for this, thanks a lot for your help :)
Just set the footer view to empty view:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
so separator will disappear for empty cells (you still keep separator for normal cells).
In objective c
[self.tableView setSeparatorColor:[UIColor myColor]];
In Swift
tableView.separatorColor = UIColor.clear

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