【问题标题】:How remove gray line on top UITableView如何删除顶部 UITableView 上的灰线
【发布时间】:2025-12-28 01:25:15
【问题描述】:

初始化 UIViewController 代码:

    self.view.backgroundColor= [UIColor whiteColor];
    CGSize boundsSize = self.view.bounds.size;
    CGRect rectTableViewFrame = CGRectMake(0, 0, boundsSize.width, boundsSize.height - 64);
    UITableView* contentTableView = [[UITableView alloc] initWithFrame:rectTableViewFrame];

    contentTableView.backgroundColor = [UIColor clearColor];
    contentTableView.separatorColor = [UIColor clearColor];
    contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

接下来添加UITableView自定义视图(header):

    CustomView* customView = [[CustomView alloc] init];
    CGRect customViewFrame = customView.frame;
    customViewFrame.origin.y = - customView.size.height;
    customView.frame = customViewFrame;

    contentTableView.contentOffset = CGPointMake(0, customViewFrame.origin.y);
    contentTableView.contentInset = UIEdgeInsetsMake(customViewFrame.frame.size.height, 0, 0, 0);
    [contentTableView addSubview: customView];

问题:当滚动contentTableView顶部customView有一条灰线...如何删除这个? 我不使用方法:

    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

但如果使用白色背景颜色的简单视图,则没有线条。

【问题讨论】:

  • 尝试评论 contentInset 行并检查一次。

标签: ios uitableview


【解决方案1】:

您可以尝试设置无线条样式,例如:

contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

并在你的底部添加一条灰线UITableViewCell

【讨论】:

  • 我用过(往上看): contentTableView.backgroundColor = [UIColor clearColor]; contentTableView.separatorColor = [UIColor clearColor]; contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  • 因为 UINavigationController 有 1px(0.5px inretina) 阴影线,而 UITableViewCell 的 1px(0.5px inretina) 灰线在点 (0, 0)。静止状态下,导航阴影隐藏单元格灰线,但实际上灰线存在。
  • [[UINavigationBar appearance]setShadowImage:[[UIImage alloc] init]]; 已删除导航栏阴影。但没有帮助...
  • 问题不在于导航,而在于 UITableViewCell 上 point(0, 0) 处的灰色顶线。可以尝试设置UITableView.separatorInsetUITableViewCell.separatorInset
  • 不起作用...尝试了 UIEdgeInsetsMake(0, 50, 0, 0);和 UIEdgeInsetsZero