如何删除第一个单元格顶部分隔符和最后一个单元格底部分隔符

时间:2015-09-19 13:29:57

标签: ios objective-c uitableview separator

我需要删除特定部分中的顶部边框形式的第一个单元格,并在特定部分中删除最后一个单元格的底部边框。

我用Google搜索完全隐藏分隔符的解决方案,但是我想避免使用它。

此外,当您在显示单元格以使用分隔符插入时,我找到了解决方案。它适用于两个单元格之间的中间分隔符,但不适用于页眉/页脚和单元格之间的分隔符。

另外,我试图在标题中看到子图层,但由于此视图是由我自己创建的,因此我没有找到分隔符视图。

可能我应该使用标题内的图层?请给我一个线索。

6 个答案:

答案 0 :(得分:3)

下面的代码帮助我从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
        }
    }

答案 1 :(得分:2)

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

}

}

最终输出是

enter image description here

答案 2 :(得分:0)

对我来说,事实证明,所有这些视图黑客攻击都比创建自己的UITableViewCell子类要复杂得多,在这个子类中你将1p高UIView作为自己的分隔符并将其隐藏在cellForRowAtIndexPath中:基于行如果行indexPath是该部分的最后一行。

一旦您了解了如何创建自定义UITableViewCells,这实际上是一项非常容易的工作。此外,它比黑客苹果设计更安全,更具前瞻性。它们为您提供适合整个系统风格的分离器。并非他们所做的一切都是为了最大限度地定制。

在分隔符的情况下,我认为最好是与他们的风格一起生活,或者只是想出你自己的分隔符视图实现。

答案 3 :(得分:0)

要删除底部插图,这对我有用!

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

答案 4 :(得分:0)

删除第一个分隔符,只需要 - 只需删除 tableHeaderView。 您可以在 viewDidLoad 中这样做:

tableView.tableHeaderView = UIView()

答案 5 :(得分:-1)

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