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.
To remove the first separator, only is needed - just to remove the tableHeaderView
.
You can do it like this in viewDidLoad
:
tableView.tableHeaderView = UIView()
Below code helps me to remove the separator from last row in a UITableView.
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
}
}
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With