How to remove first cell top separator and last cell bottom separator
Asked Answered
M

7

8

I need to remove top border form first cell in a particular section and bottom border form last cell in a particular section.

I googled solution for complete hiding separators from tableView, but I'd like to avoid it.

Also I've found solution when you get event when the cell will be displaying to play with separator insets. It works for middle separators, between two cells, but not for separator between header/footer and cell.

Plus I've tried to see sublayers in the header, but since this view is created by myself, I didn't found there separator view.

May be I should work with layers inside header? Please, give me a clue.

Mauretta answered 19/9, 2015 at 13:29 Comment(1)
Did you find a solution for this Anton?Surfing
E
17

To remove the first separator, only is needed - just to remove the tableHeaderView. You can do it like this in viewDidLoad:

tableView.tableHeaderView = UIView()
Econometrics answered 2/4, 2021 at 8:56 Comment(1)
Cool, this works for tables without section headers. But if the table has section headers, the cells after the headers still have a top separator, which doesn't follow the tableView.separatorInsets.Crumpled
F
6

Below code helps me to remove the separator from last row in a UITableView.

Swift 3.0

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) {
            cell.separatorInset.left = cell.bounds.size.width
        }
    }
Furlong answered 18/5, 2017 at 21:10 Comment(0)
C
2

in your ViewDiDLoad

   - (void)viewDidLoad {
[super viewDidLoad];
yourTableview.tableFooterView=[[UITableView alloc]initWithFrame:CGRectZero];

}


- (UIEdgeInsets)layoutMargins
{
return UIEdgeInsetsZero;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [tableView setSeparatorInset:UIEdgeInsetsZero];
}

if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
    [tableView setLayoutMargins:UIEdgeInsetsZero];
}

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
    [cell setLayoutMargins:UIEdgeInsetsZero];
}


if ( indexPath.row ==  tableData.count-2 ) {


    cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(tableView.bounds));

}

else  {

   // do nothing

}

}

the final output is

enter image description here

Conductor answered 21/9, 2015 at 7:5 Comment(0)
D
1

For me it turned out that all this view hacking is way more complicated than just creating your own UITableViewCell subclass where you put a 1p high UIView as your own separator in it and hide it in cellForRowAtIndexPath: based on the fact if the row of the indexPath is the last row in the section.

Thats actually a pretty easy job to do once you understand how to create custom UITableViewCells. Also it is way safer and future proof than hacking Apples design. They offer you a separator which fits the overall system style. Not everything they do is intended to be customized to its max.

In the case of separator, I think it's just the best to either live with their style or just come up with your own implementation of a separator view.

Data answered 21/5, 2017 at 2:51 Comment(1)
Such an underrated answer.. Some solutions I read using timer.. lol.. How hard is it to admit that only good solution is customising cell itself..Torment
M
0

To remove bottom inset, this worked for me!

cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, self.tableView.bounds.width)

Mayers answered 4/4, 2019 at 15:59 Comment(0)
T
0

Swift 5

Hide separator for the first cell and the last cell:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == 0 || indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
        cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: tableView.bounds.width)
    }
}
Trial answered 24/8, 2021 at 15:12 Comment(1)
When I tried this, it hid the bottom separator from the first cell, not the top separator.Crumpled
Z
-2
- (void)layoutSubviews {
    [super layoutSubviews];

    for (UIView *subview in self.contentView.superview.subviews) {
        if ([NSStringFromClass(subview.class) hasSuffix:@"SeparatorView"] && subview.frame.origin.x == 0 ) {
            subview.hidden = YES;
        }else
        {
            subview.hidden = NO;
        }
    }
}
Zellers answered 9/2, 2017 at 8:34 Comment(1)
I would actually not use this method, since it relies on an internal class name in UIKit.Martinet

© 2022 - 2025 — McMap. All rights reserved.