Hide remove separator line if UITableViewCells are empty
You can hide UITableView
's standard separator line by using any one of the below snippets of code.
The easiest way to add a custom separator is to add a simple UIView
of 1px height:
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
separatorLineView.backgroundColor = [UIColor clearColor]; // set color as you want.
[cell.contentView addSubview:separatorLineView];
OR
self.tblView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,370) style:UITableViewStylePlain];
self.tblView.delegate=self;
self.tblView.dataSource=self;
[self.view addSubview:self.tblView];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
v.backgroundColor = [UIColor clearColor];
[self.tblView setTableHeaderView:v];
[self.tblView setTableFooterView:v];
[v release];
OR
- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// This will create a "invisible" footer
return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
// To "clear" the footer view
return [[UIView new] autorelease];
}
OR
And also check nickfalk's answer, it is very short and helpful too.
And you should also try this single line,
self.tableView.tableFooterView = [[UIView alloc] init];
Not sure but it's working in all the version of iOS that I checked, with iOS 5 and later, up to iOS 7.
Can I force a UITableView to hide the separator between empty cells?
You can achieve what you want by defining a footer for the tableview. See this answer for more details:Eliminate Extra separators below UITableView
Eliminate extra separators below UITableView
Interface builder (iOS 9+)
Just drag a UIView to the table. In storyboard, it will sit at the top below your custom cells. You may prefer to name it "footer".
Here it is shown in green for clarity, you'd probably want clear color.
Note that by adjusting the height, you can affect how the "bottom bounce" of the table is handled, as you prefer. (Height zero is usually fine).
To do it programmatically:
Swift
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.tableFooterView = UIView()
}
Objective-C
iOS 6.1+
- (void)viewDidLoad
{
[super viewDidLoad];
// This will remove extra separators from tableview
self.tableView.tableFooterView = [UIView new];
}
or if you prefer,
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Historically in iOS:
Add to the table view controller...
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// This will create a "invisible" footer
return CGFLOAT_MIN;
}
and if necessary...
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [UIView new];
// If you are not using ARC:
// return [[UIView new] autorelease];
}
Remove separator line for only one cell
On iOS 8 you need to use:
cell.layoutMargins = UIEdgeInsetsZero
If you want to be compatible with iOS 7 as well you should do following:
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
ADD: If previous didn't work - use this. (from answer below)
cell.separatorInset = UIEdgeInsetsMake(0, CGFLOAT_MAX, 0, 0);
If none of above worked, you can do:
self.tableView.separatorColor = [UIColor clearColor];
but this will leave 1 pixel empty space, not really removing a line, more making it transparent.
Hide separator line on one UITableViewCell
in viewDidLoad
, add this line:
self.tableView.separatorColor = [UIColor clearColor];
and in cellForRowAtIndexPath
:
for iOS lower versions
if(indexPath.row != self.newCarArray.count-1){
UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 2)];
line.backgroundColor = [UIColor redColor];
[cell addSubview:line];
}
for iOS 7 upper versions (including iOS 8)
if (indexPath.row == self.newCarArray.count-1) {
cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
}
UITableViewCell hide separator using separatorInset fails in iOS 11
If you are not keen on adding a custom separator to your UITableViewCell
I can show you yet another workaround to consider.
How it works
Because the color of the separator is defined on the UITableView
level there is no clear way to change it per UITableViewCell
instance. It was not intended by Apple and the only thing you can do is to hack it.
The first thing you need is to get access to the separator view. You can do it with this small extension.
extension UITableViewCell {
var separatorView: UIView? {
return subviews .min { $0.frame.size.height < $1.frame.size.height }
}
}
When you have an access to the separator view, you have to configure your UITableView
appropriately. First, set the global color of all separators to .clear
(but don't disable them!)
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorColor = .clear
}
Next, set the separator color for each cell. You can set a different color for each of them, depends on you.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SeparatorCell", for: indexPath)
cell.separatorView?.backgroundColor = .red
return cell
}
Finally, for every first row in the section, set the separator color to .clear
.
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
cell.separatorView?.backgroundColor = .clear
}
}
Why it works
First, let's consider the structure of the UITableViewCell
. If you print out the subviews
of your cell you will see the following output.
<UITableViewCellContentView: 0x7ff77e604f50; frame = (0 0; 328 43.6667); opaque = NO; gestureRecognizers = <NSArray: 0x608000058d50>; layer = <CALayer: 0x60400022a660>>
<_UITableViewCellSeparatorView: 0x7ff77e4010c0; frame = (15 43.5; 360 0.5); layer = <CALayer: 0x608000223740>>
<UIButton: 0x7ff77e403b80; frame = (0 0; 22 22); opaque = NO; layer = <CALayer: 0x608000222500>>
As you can see there is a view which holds the content, the separator, and the accessory button. From this perspective, you only need to access the separator view and modify it's background. Unfortunately, it's not so easy.
Let's take a look at the same UITableViewCell
in the view debugger. As you can see, there are two separator views. You need to access the bottom one which is not present when the willDisplay:
is called. This is where the second hacky part comes to play.
When you will inspect these two elements, you will see that the first (from the top) has a background color set to nil
and the second has a background color set to the value you have specified for entire UITableView
. In this case, the separator with the color covers the separator without the color.
To solve the issue we have to "reverse" the situation. We can set the color of all separators to .clear
which will uncover the one we have an access to. Finally, we can set the background color of the accessible separator to what is desired.
Remove UITableView separator line
Objective-C :
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
Swift:
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
Swift 5.0 renamed it in :
self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
Apply the line in viewDidLoad() method.
If you want to do it from nib file, set the tableView's Separator property to None
Remove first line separator of UItableView Cell
Within your cellForRowAt
delegate function you can use:
if indexPath.row == 0 {
cell?.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: .greatestFiniteMagnitude)
}
Is there a way to remove the separator line from a single cell in UITableView?
Your best bet is probably to set the table's separatorStyle
to UITableViewCellSeparatorStyleNone
and manually adding/drawing a line (perhaps in tableView:cellForRowAtIndexPath:
) when you want it.
Remove single separator line from UITableViewCell
The easiest way is: don't use built-in separator lines. Draw the line yourself (as part of the cell) in each row where you want one, and don't draw it (i.e. remove it) in each row where you don't want it.
Related Topics
Uiimageview Missing Images in Launch Screen on Device
iOS Autolayout: Two Buttons of Equal Width, Side by Side
Uiview Vertical Flip Animation
How to Make iPhone Vibrate Using Swift
How to Display the Default iOS 6 Share Action Sheet with Available Share Options
Ask for User Permission to Receive Uilocalnotifications in iOS 8
Sort Array of Dictionaries by Key Value
Why Can't I Invert My Image Back to Original with Cifilter in My Swift iOS App
Get Random Child from Firebase Database
iOS 7 Parallax Effect in My View Controller
Implementing Fast and Efficient Core Data Import on iOS 5
Xcode 10: a Valid Provisioning Profile for This Executable Was Not Found
Images Can't Contain Alpha Channels or Transparencies
Dashed Line Border Around Uiview
Custom Cell Row Height Setting in Storyboard Is Not Responding