【问题标题】:Can I force a UITableView to hide the separator between empty cells? [duplicate]我可以强制 UITableView 隐藏空单元格之间的分隔符吗? [复制]
【发布时间】:2010-12-10 16:13:54
【问题描述】:

当使用具有足够多单元格的普通样式 UITableView 时,UITableView 无法在不滚动的情况下全部显示,单元格下方的空白区域中不会出现分隔符。如果我只有几个单元格,则它们下方的空白区域包括分隔符。

有没有办法强制UITableView 删除空白空间中的分隔符?如果不是,我将不得不加载一个自定义背景,并为每个单元格绘制一个分隔符,这将使得继承行为变得更加困难。

我发现了一个有点类似的问题here,但我不能在我的实现中使用分组的UITableView

【问题讨论】:

    标签: ios objective-c cocoa-touch uitableview


    【解决方案1】:

    对于斯威夫特:

    self.tableView.tableFooterView = UIView(frame: CGRectZero)
    

    对于最新的 Swift:

    self.tableView.tableFooterView = UIView(frame: CGRect.zero)
    

    【讨论】:

    • 在 Swift 中使用 CGRect.zero
    • 这对我有用。谢谢。
    【解决方案2】:

    对于斯威夫特:

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()  // it's just 1 line, awesome!
    }
    

    【讨论】:

    • 更好的用法:tableView.tableFooterView = UIView(frame: CGRect.zero))
    • @ayalcinkaya 为什么比UIView()更好?
    • UIView() 等价于 UIView(frame: CGRect.zero)
    • 像魅力一样工作!谢谢!
    【解决方案3】:

    斯威夫特版本

    最简单的方法是设置 tableFooterView 属性:

    override func viewDidLoad() {
        super.viewDidLoad()
        // This will remove extra separators from tableview
        self.tableView.tableFooterView = UIView(frame: CGRectZero)
    }
    

    【讨论】:

    • 对于 Swift 3 将是:tableView.tableFooterView = UIView(frame: CGRect.zero)
    • 或者只是self.tableView.tableFooterView = UIView()(不需要明确的框架)。
    【解决方案4】:

    适用于 iOS 7.* 和 iOS 6.1

    最简单的方法是设置tableFooterView属性:

    - (void)viewDidLoad 
    {
        [super viewDidLoad];
    
        // This will remove extra separators from tableview
        self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    }
    

    对于以前的版本

    您可以将其添加到您的 TableViewController(这将适用于任意数量的部分):

    - (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】:

      如果你使用 iOS 7 SDK,这很简单。

      只需在 viewDidLoad 方法中添加这一行:

      self.yourTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
      

      【讨论】:

        【解决方案6】:

        您可以通过为 tableview 定义页脚来实现您想要的。更多详情请看这个答案:Eliminate Extra separators below UITableView

        【讨论】:

          【解决方案7】:

          以下方法对我来说非常有效:

          - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
          
          CGRect frame = [self.view frame];
          frame.size.height =  frame.size.height - (kTableRowHeight * numberOfRowsInTable);
          
          UIView *footerView = [[UIView alloc] initWithFrame:frame];
          return footerView; }
          

          其中 kTableRowHeight 是我的行单元格的高度,numberOfRowsInTable 是我在表格中的行数。

          希望对你有帮助,

          布伦顿。

          【讨论】:

            【解决方案8】:

            我使用以下:

            UIView *view = [[UIView alloc] init];
            myTableView.tableFooterView = view;
            [view release];
            

            在 viewDidLoad 中执行。但是你可以在任何地方设置它。

            【讨论】:

              【解决方案9】:

              使用 Daniel 的链接,我做了一个扩展以使其更实用:

              //UITableViewController+Ext.m
              - (void)hideEmptySeparators
              {
                  UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
                  v.backgroundColor = [UIColor clearColor];
                  [self.tableView setTableFooterView:v];
                  [v release];
              }
              

              经过一些测试,我发现大小可以为 0,并且效果也很好。所以它不会在表格末尾添加某种边距。所以感谢 wkw 的这次黑客攻击。我决定在这里发布,因为我不喜欢重定向。

              【讨论】:

              • 需要设置背景色吗?
              【解决方案10】:

              将表的 separatorStyle 设置为 UITableViewCellSeparatorStyleNone(在代码或 IB 中)应该可以解决问题。

              【讨论】:

              • 如果我这样做,我会丢失所有的分隔符,这不是我正在寻找的行为。如果表格没有足够的单元格来填满整个屏幕,表格视图会自动将分隔符绘制到空白区域,但是如果有足够的单元格来填满屏幕,则在最后一个单元格下方的空白处没有绘制分隔符。我可以关闭分隔符,只使用带有分隔符的单元格背景,但是我正在向我的项目添加另一个资产,这可能会使将来的更新更加困难。
              • 我明白了。我不相信您只能在不存在细胞的情况下移除分隔符。想到的唯一其他简单解决方案是让您的单元类继承自一个非常小的 UITableViewCell 子类,该子类仅绘制分隔线。这是一个额外的复杂部分,但不需要单独的图像。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-03-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多