我需要从特定部分中删除顶部边框形式的第一个单元格,并在特定部分中删除最后一个单元格的底部边框。
我用google搜索解决方案来完成从tableView中隐藏分隔符,但我想避免使用它。
当我在显示单元格时使用分隔符插件时,我找到了解决方案。它适用于两个单元格之间的中间分隔符,但不适用于页眉/页脚和单元格之间的分隔符。
另外,我试图在标题中看到子图层,但由于此视图是由我自己创建的,因此我没有找到分隔符视图。
可能我应该使用标题内的图层?拜托,给我一个线索。
下面的代码帮助我从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
}
}
在你的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
}
}
最终的输出是
对我来说,事实证明,所有这些视图黑客攻击都比创建你自己的UITableViewCell子类要复杂得多,在这个子类中你将1p高UIView作为你自己的分隔符并将其隐藏在cellForRowAtIndexPath中:基于indexPath行的事实是该部分的最后一行。
一旦你理解了如何创建自定义UITableViewCells,这实际上是一件非常容易的事。此外,它比黑客苹果设计更安全,更具前瞻性。它们为您提供适合整个系统风格的分离器。并非他们所做的一切都是为了最大限度地定制。
在分隔符的情况下,我认为最好是与他们的风格一起生活,或者只是想出你自己的分隔符视图实现。
要删除底部插图,这对我有用!
cell.separatorInset = UIEdgeInsetsMake(0,0,0,self.tableView.bounds.width)
- (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;
}
}
}