【问题标题】:Hide remove separator line if UITableViewCells are empty [duplicate]如果 UITableViewCells 为空,则隐藏删除分隔线 [重复]
【发布时间】:2013-01-05 19:32:25
【问题描述】:

我有一个UITableView,其中只有 3 行,我可以看到这 3 行。问题是空的单元格:我可以看到那里的线条。我不想看到那些行。

知道如何删除这些行吗?

下面是我要找的图片。

【问题讨论】:

  • 创建屏幕截图,按下command+shift+4,鼠标光标变为加号,点击并拖动所需区域,当U松开鼠标按钮时,图像将被捕获并保存在桌面中。
  • 我喜欢截图的背景是带有类似问题的 SO 页面,好像在说“哦,SO 的领主,请原谅我。我搜索过,我真的搜索过”:D

标签: ios uitableview cocoa-touch


【解决方案1】:

您可以使用以下任何一种 sn-ps 代码来隐藏UITableView 的标准分隔线。 添加自定义分隔符最简单的方法是添加一个简单的UIView,高度为 1px:

UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
separatorLineView.backgroundColor = [UIColor clearColor]; // set color as you want.
[cell.contentView addSubview:separatorLineView];

    self.tblView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,370) style:UITableViewStylePlain];
    self.tblView.delegate=self;
    self.tblView.dataSource=self;
    [self.view addSubview:self.tblView];

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
    v.backgroundColor = [UIColor clearColor];
    [self.tblView setTableHeaderView:v];
    [self.tblView setTableFooterView:v];
    [v release];

- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    // This will create a "invisible" footer
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    // To "clear" the footer view
    return [[UIView new] autorelease];
}

还要检查nickfalk's answer,它非常简短且很有帮助。 你也应该试试这条线,

self.tableView.tableFooterView = [[UIView alloc] init];

不确定,但它适用于我检查过的所有 iOS 版本,包括 iOS 5 及更高版本,直至 iOS 7。

【讨论】:

  • 请注意,使用第二个选项将删除最后一个单元格的分隔线,这可能不是您想要的。第一个选项将保留分隔线,但我建议只使用以下代码:[self.tableView setTableFooterView:[UIView new]]; 用于不可见的页脚,如下面的答案所述。
  • @iPatel 我正在使用第三个选项,当我关注最后一个表格单元格时,会出现分隔符...对此有什么解决办法吗?
  • 有人介意为第一个选项提供一点上下文吗?我不知道应该去哪里。
  • @Pepedou 您正在修改一个单元格,因此,如果您希望将其应用于单个实例,那么您可以在 cellForAtIndex 中执行,或者如果您希望将其应用于 *tableViewCell` 类然后你会在它的班级里这样做,所以它的所有实例都会受到影响......
【解决方案2】:

透明的UIView 作为tableView 1px 高度的页脚就可以了。

UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];
v.backgroundColor = [UIColor clearColor];
[self.tableView setTableFooterView:v];

【讨论】:

  • OR : Swift on viewDidLoad() self.tableView.tableFooterView = UIView(frame: CGRect.zero)
【解决方案3】:

比 Andrey Z 的回复还要简单: 只需在 UITableView 类中创建一个虚假的 tableFooterView:

self.tableFooterView = [UIView new]; // to hide empty cells

和斯威夫特:

tableFooterView = UIView()

【讨论】:

  • 它在 iOS 7 中不起作用。@iPatel 解决方案效果很好。
  • [self.tableView setTableFooterView:[UIView new]];
  • @RedDevil 它在我的 iOS 7 中运行良好。
  • @RedDevil 你测试过吗,它在 iOS 7.0 中工作。
  • 如果您不想让最后一个单元格的分隔符可见,则它不起作用。它将删除每个后续单元格的分隔符,但对于您拥有的最后一个单元格,分隔符仍然存在。使用@iPatel 的第三个选项以获得最佳结果。
【解决方案4】:

使用此代码删除空单元格的分隔线。

 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// This will create a "invisible" footer
return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
     return [UIView new];

    // If you are not using ARC:
    // return [[UIView new] autorelease];
}   

【讨论】:

  • 使用后,最后一行的分隔符不可见。有什么想法吗?
【解决方案5】:
tableForBrands.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

【讨论】:

【解决方案6】:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

【讨论】:

【解决方案7】:

建议的答案都不适合我的类似问题。在 tableView 委托中实现这个方法终于奏效了:

- (void)tableView:(UITableView *)tableView
  willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

【讨论】:

  • 这将禁用所有分隔符。根本不是解决问题的办法。您还为每个单元格的显示调用它 - 这确实不是一个好的解决方案。如果要运行代码,请将其放在 viewDidLoad 中。
  • 这将删除以下行:这是核心问题。如果您检查 OP 的附加图像:紫色显示:“我想删除下面的行”。您始终可以在填充的单元格上绘制分隔符。由于 UITableViewCell 的 layoutSubviews 中的底层错误,简单地将它放在 viewDidLoad 中实际上在 iOS > 7 中不起作用。即使将分隔符颜色设置为 tableView 的 backgroundColor 的 clearColor 也不起作用。即使将它放在 cellForRowAtIndexPath 中也不起作用......因为 layoutSubviews 发生在之后:就在显示单元格之前。
  • 我刚试过。正如预期的那样,它删除了所有分隔符,因为它是 tableView 的属性而不是单元格。
  • tableView 有两种类型:plain 和 grouped。它适用于一种类型,但不适用于另一种类型。至少在某些版本的 iOS 上。
【解决方案8】:

如果您使用 Storyboards,您只需将 UIView 拖放到单元格下方的 UITableView 中,并将其高度设置为 0。(仅在 iOS 8 项目中测试过)

【讨论】:

    【解决方案9】:

    请尝试以下代码:

    self.tblView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    if ([self.tblView respondsToSelector:@selector(setSeparatorInset:)])
    {
        [self.tblView setSeparatorInset:UIEdgeInsetsZero];
    }
    // write in view did load
    

    【讨论】:

      【解决方案10】:

      我猜这就是你要找的。​​p>

      - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
          return 1.0f;
      }
      

      【讨论】:

        【解决方案11】:

        您可以将表格视图设置为分组而不是简单的 - 这会稍微改变外观,但至少会删除多余的行。

        我遇到了这个确切的问题,最终更改为分组视图。看起来好多了。

        【讨论】:

          【解决方案12】:

          更新了 swift 和 iOS 9 的答案。这适用于 .Plain 品种的表格视图。

           tableView.tableFooterView = UIView()
          

          【讨论】:

          【解决方案13】:

          之前的一些建议包含一个很大的概念错误:

          如果你这样做:

          [单元格添加子视图:....

          即使一个单元格被“重用”,你也会为分隔线添加一个新的子视图!

          通过两种方式避免它:

          a) 使用标签,并且: 1)请求该标签的子视图 让分隔符 = cell.viewWithTag(TAG) ... 2)如果存在,不要添加另一个子视图 3) 如果不存在,则添加并标记它。

          b) 创建一个自定义视图并在自定义单元格的“init”“awakeFromNib”中添加您的自定义分隔符。

          a) 的代码:

           if let divider = cell.viewWithTag(DIVIDER_TAG) as? UIView{
                      // nothing.. eventually change color bases in IndexPath...
                  }else{
                      let frame = CGRectMake(0, cell.frame.height-1, cell.frame.width, 1)
                      divider.tag = DIVIDER_TAG
                      divider.backgroundColor = UIColor.redColor()
                      cell.addSubview(divider)
                  }
          

          【讨论】:

            【解决方案14】:

            viewForFooterInSection 中返回一个空的UIView() 对我有用:

            override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
                    return UIView()
                }
            

            【讨论】:

              【解决方案15】:

              cellForRowAtIndexPath 内部

              let separatorLineView:UIView = UIView(frame: CGRectMake(0,0,self.tableview.bounds.width,0.5))
              separatorLineView.backgroundColor = tableView.separatorColor
              cell!.contentView.addSubview(separatorLineView)
              

              【讨论】:

                【解决方案16】:

                迅捷解决方案: tableView.tableFooterView = UIView(frame: CGRectZero)

                在 Xcode 7.2 上工作

                【讨论】:

                猜你喜欢
                • 2017-08-24
                • 2013-12-01
                • 2021-11-24
                • 1970-01-01
                • 1970-01-01
                • 2012-01-08
                • 2013-04-02
                • 2020-11-06
                • 2014-01-21
                相关资源
                最近更新 更多