After inspecting the view hierarchy, it seems each UITableViewCell has only three subviews: the content view (UITableViewCellContentView), and two separator views (_UITableViewCellSeparatorView). I'm not a fan of dynamic class instantiation from NSStrings (and neither is Apple 😉). However, because the contentView of a UITableViewCell is accessible without using private APIs, the solution turns out to be pretty easy.
The idea is just to iterate through the subviews of your UITableViewCell, and remove any views that aren't the contentView:
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let subviews = cell.subviews
if subviews.count >= 3 {
for subview in subviews {
if subview != cell.contentView {
subview.removeFromSuperview()
break
}
}
}
}
tableView(:willDisplayCell:forRowAtIndexPath:) is called multiple times during table view rendering, so the above keeps track of state by checking how many subviews the cell has. If it has three subviews, both separators are still intact. It also only removes one of the separators, but you can remove both by removing the break. You can also specify whether to remove the top separator or the bottom separator by checking the subview's frame. If the frame's y-axis origin is 0, that's the top separator. If it's not 0, it's the bottom.
Hope this helps!
Swift 4, Swift 4.2 Update:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let subviews = cell.subviews
guard subviews.count >= 3 else {
return
}
for subview in subviews where NSStringFromClass(subview.classForCoder) == "_UITableViewCellSeparatorView" {
subview.removeFromSuperview()
}
}
I was not able to find some better way than this. But it is working for me greatly. Last tried with Xcode Version 7.3.1 (7D1014). This procedure was done through storyboard.
Basically I add a UIView of 0.5 pt Height on the UITableViewCell and then set a background color for that UIView. Set parent UITableView's Separator as None.
Here is the details:
Considering you already set your UITableViewCell, custom or default. On the very first stage set the UITableView's separator as None.
Next add a UIView of 1 pt Height and set the Background as you need, on my case it is Red.
Start setting the constraints. The problem is to set the height of the UIView as 0.5 pt. This is the only Problematic issue for this workflow.
UIView with 0.5 pt Height:
Sharing the way to set 0.5 pt height of the UIView.
First(1) pin the view and then(2) set the height as 0.5. Press Enter.
Finally your UIView will look similar like following.
I was not able to set the height as 0.5 other than this way.
That is a really old question, still it's one of the first entries on google when searching for how to remove the top and bottom separators for each section.
After some investigation, I found out that there is no way and just no intention from Apple to make this somehow happen without stupidly complicated hacks of the view hierarchy.
Fortunately there is a absolutely simple and easy way of achieving such a look:
I say simple but for a beginner, this might be difficult because of a lack of understanding how UITableView works and how to implement your own cells. Let me try to explain it:
Create a UITableViewCell subclass
In your cell, create a @IBOutlet weak var separatorView: UIView! property
In your Storyboard, select the tableview cell and select your own cell as the class which backs up the cell. Note: You do not have to use custom style. You still can use Basic, Subtitle, etc.
Set your UITableViews Separator Style to None to hide the default separator
Drag a UIView onto your cell and resize it so it is on the bottom (or the top) of the cell. Use the Size Inspectors Autoresizing to pin it to start/end/bottom and give it a flex width (or Autolayout but thats just over the top in this case)
Connect the view to the outlet of your cell class
In your UITableViewDataSources cellForRowAtIndexPath: set the isHidden property of your custom separator based on if the indexPath.row is the last row (or the first, if your view is at the top) in the section
Yes, it is some work but there is just no way to something for free when coding. In the end, we are programmers and it is up to us to do the coding. It's always better to spend the 5-10 minutes setting this up than just copy pasting some hacky code which might not continue to work in the future when Apple decides to change the view hierarchy.
It was actually more work to write this answer than implementing the separator. I as well was searching for a easy and quick solution but in the end there just wasn't a good enough one which I felt was worth using.
I hope you also feel skeptical when you see for-loops iterating over subviews of cells to hide or even remove views at runtime from the view hierarchy Apple provides you, when there is an easy, versatile, stable and future proof solution right around the corner. 7 little steps is really all you need and they are easy to understand.
I had a similar issue, where I had a Grouped UITableView with custom cells, all designed with Interface Build as .xib files. The cells had white backgrounds, removed the default separators, and added my own ones. I also had custom Header Views for each section. I set the height of those Header Views to be 44 (could be anything...). There was a 1 point height view between my sections, which seemed weird. Apparently, the system adds some clear background view between the sections, even if we specify the custom height to be, say 44, and the custom Header View we return has a white (or some concrete background color). The color we see behind that clear view is the color of of the Table View's background actually. In my case, both the table views and the cells had to be of white color, and setting the background of table view to be white solved the problem (at least visually, but that's what I wanted anyway). Second solution would be to keep the Table View's style as plain, but implement the UITableView delegate method to return 2 or more sections, and also create custom headers if you need to. But that will make header views to stick to the top for a while (while scrolling), until the next section's header view gets closer to it, and then it starts going up too (and that may not be what you really want, but there may be an easy way to fix that, not sure though).
Google, even in 2018, is serving this page as the top result for this question. I didn't have any luck in iOS 11 with any of the provided answers, so here's what I came up with:
extension UITableViewCell {
func removeSectionSeparators() {
for subview in subviews {
if subview != contentView && subview.frame.width == frame.width {
subview.removeFromSuperview()
}
}
}
}
Calling .removeSectionSeparators() on any UITableViewCell instance will now take care of the problem. In my case at least, the section separators are the only ones with the same width as the cell itself (as the other ones are all indented).
The only question left is from where we should call it. You'd think willDisplayCell would be the best choice, but I discovered that the initial call occurs before the separator views themselves are generated, so no dice.
I ended up putting this in my cellForRowAtIndexPath method just before I return a reloaded cell:
For removing the top and bottom part of separator line for each section. Add this to your static cell.
override func layoutSubviews() {
super.layoutSubviews()
//Get the width of tableview
let width = subviews[0].frame.width
for view in subviews where view != contentView {
//for top and bottom separator will be same width with the tableview width
//so we check at here and remove accordingly
if view.frame.width == width {
view.removeFromSuperview()
}
}
}
override func layoutSubviews(){
super.layoutSubviews()
for subview in subviews where (subview != contentView && abs(subview.frame.width - frame.width) <= 0.1 && subview.frame.height < 2) {
subview.removeFromSuperview() //option #1 -- remove the line completely
//subview.frame = subview.frame.insetBy(dx: 16, dy: 0) //option #2 -- modify the length
}
}
I want to thank @cook for this solution, as I built on top of it. I had some issues with their solution:
it was removing the default highlight/selected background view, so I added an extra check on the subview's height.
I put my code in layoutSubviews() and haven't had a single issue.
I implemented an approximation between two CGFloats instead of using the equality operator ==, which sounds error-prone to me.
I added the "option #2" in the code, as that's the solution I was personally looking for (I wanted to maintain the separator, but I wanted it to be at the same indentation level as the regular cell separators, in my case a value of 16).
-(void)layoutSubviews{
[super layoutSubviews];
//for iOS10~iOS13: only remove section head foot line
for (UIView * v in self.subviews) {
if ( v != self.contentView &&
(v.frame.size.width == self.frame.size.width)){
[v removeFromSuperview];
}
}
}
if wan to remove all line:
for (UIView * v in self.subviews) {
if (v != self.contentView){
[v removeFromSuperview];
}
}