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

I need to remove the upper border of the shape of the first cell in a specific section and the lower border of the shape of the last cell in a specific section.

I found a solution to completely hide the delimiters from the tableView, but I would like to avoid it.

I also found a solution when you get an event when a cell will be displayed for playback using split attachments. It works for middle separators, between two cells, but not for the separator between the header and footer and the cell.

Plus, I tried to see sublayers in the header, but since this view was created by me, I did not find a separator there.

Maybe I should work with layers inside the header? Please give me the key.

+6
source share
5 answers

The code below helps me remove the separator from the last line 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
        }
    }
+3
source

in 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

}

}

final result

enter image description here

+2
source

, , UITableViewCell, 1p UIView cellForRowAtIndexPath: , indexPath - .

, , UITableViewCells. , . , . , , .

, .

0

, !

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

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

Source: https://habr.com/ru/post/1608134/


All Articles