Anton Lovchikov
Anton Lovchikov

Reputation: 523

How to remove first cell top separator and last cell bottom separator

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.

Upvotes: 8

Views: 15158

Answers (7)

oskarko
oskarko

Reputation: 4178

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)
    }
}

Upvotes: 0

Mihail Salari
Mihail Salari

Reputation: 1571

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

tableView.tableHeaderView = UIView()

Upvotes: 17

Madhurya Gandi
Madhurya Gandi

Reputation: 892

To remove bottom inset, this worked for me!

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

Upvotes: 0

user2991638
user2991638

Reputation: 111

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
        }
    }

Upvotes: 6

xxtesaxx
xxtesaxx

Reputation: 6419

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.

Upvotes: 1

zhuoming
zhuoming

Reputation: 17

- (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;
        }
    }
}

Upvotes: -2

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

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

Upvotes: 2

Related Questions