如何移除第一个单元格的顶部分隔线和最后一个单元格的底部分隔线。

7

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

我在谷歌上搜索了完全隐藏tableView分隔符的解决方案,但我想避免这种方法。

另外,我找到了一种解决方案,可以在单元格即将显示时获取事件以操作分隔符插图。它适用于两个单元格之间的中间分隔符,但不适用于标题/页脚和单元格之间的分隔符。

此外,我尝试查看标题中的子层,但由于这个视图是由我自己创建的,所以我没有找到分隔符视图。

也许我应该在标题内部使用图层?请给我一些提示。


你找到解决办法了吗,Anton? - Abhinav
7个回答

17
为了 移除第一个分隔符,只需要删除 tableHeaderView 即可。 您可以在 viewDidLoad 中这样做:
tableView.tableHeaderView = UIView()

1
很棒,这对没有区域标题的表格有效。但是如果表格有区域标题,那么标题后面的单元格仍然有一个顶部分隔符,它不遵循tableView.separatorInsets。 - arlomedia

6
以下代码可以帮助我从UITableView的最后一行中移除分隔符。

Swift 3.0

```swift tableView.tableFooterView = UIView() ```
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

对我来说,所有这些视图操作比创建自己的UITableViewCell子类要复杂得多,在其中放置一个高度为1p的UIView作为自己的分隔符,并根据indexPath的行是否是该部分的最后一行,在cellForRowAtIndexPath:中将其隐藏。

一旦您了解如何创建自定义UITableViewCells,这实际上是一项相当容易的工作。而且,它比操作苹果设计更加安全和具有未来性。他们提供了适合整个系统样式的分隔线。并不是他们所做的每件事都旨在最大程度地定制。

对于分隔线,我认为最好要么接受他们的样式,要么自己设计一个分隔符视图的实现。


1
这是一个被低估的答案.. 我读到一些使用计时器的解决方案..哈哈.. 承认只有定制单元格本身才是好的解决方案有多难? - Kautsya Kanu

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

0

要移除底部插图,这个方法对我有效!

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


0

Swift 5

隐藏第一个单元格和最后一个单元格的分隔线:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == 0 || indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
        cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: tableView.bounds.width)
    }
}

4
我尝试了这个方法,它隐藏了第一个单元格底部的分隔线,而不是顶部的分隔线。 - arlomedia

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

我实际上不会使用这种方法,因为它依赖于UIKit中的内部类名。 - vrutberg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接