问题
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.
回答1:
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
}
}
回答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
回答3:
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.
回答4:
To remove bottom inset, this worked for me!
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, self.tableView.bounds.width)
回答5:
- (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;
}
}
}
来源:https://stackoverflow.com/questions/32668797/how-to-remove-first-cell-top-separator-and-last-cell-bottom-separator