Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 888
Anton Lovchikov Avatar asked Sep 19 '15 13:09

Anton Lovchikov


4 Answers

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

tableView.tableHeaderView = UIView()
like image 107
Mihail Salari Avatar answered Nov 14 '22 23:11

Mihail Salari


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
        }
    }
like image 22
user2991638 Avatar answered Nov 14 '22 23:11

user2991638


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

like image 32
Anbu.Karthik Avatar answered Nov 14 '22 21:11

Anbu.Karthik


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.

like image 44
xxtesaxx Avatar answered Nov 14 '22 23:11

xxtesaxx