【问题标题】:Eliminate extra separators below UITableView消除 UITableView 下面多余的分隔符
【发布时间】:2010-11-25 02:02:26
【问题描述】:

当我设置一个有 4 行的表格视图时,在填充的行下方仍然有额外的分隔线(或额外的空白单元格)。

如何删除这些单元格?

【问题讨论】:

    标签: ios uitableview cocoa-touch


    【解决方案1】:

    这是不使用分组表格样式的另一种方法,您可能猜不到。将 页眉和 页脚添加到表 (可能一个或另一个就足够了,尚未检查) 会导致分隔符从填充/空白行中消失。

    我偶然发现了这一点,因为我希望在表格的顶部和底部留出一点空间,以减少按下按钮的风险,而不是手指多肉的表格单元格。这是一种将空白视图作为页眉和页脚粘贴的方法。使用任何你喜欢的高度,你仍然可以消除额外的分隔线。

    - (void) addHeaderAndFooter
    {
        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
        v.backgroundColor = [UIColor clearColor];
        [self.myTableView setTableHeaderView:v];
        [self.myTableView setTableFooterView:v];
        [v release];
    }
    

    为了响应@Casebash,我回到了我的应用程序中的代码(iTunes 商店中的“AcmeLists”列表管理器......)并短路了 addHeaderAndFooter 方法来验证。 没有它,我有额外的行分隔符;使用代码,我有您在此窗口快照中看到的内容:no table row separators picture。所以我不确定为什么它对你不起作用。此外,对我来说,在表格视图上拥有任何自定义页脚都必须停止为其下方的空白行绘制行分隔符。那将是可怕的。作为参考,我查看了行数多于屏幕上无法查看的表格,然后查看了有两行的表格。在这两种情况下,都没有多余的分隔符。

    也许您的自定义视图实际上并未添加。要检查这一点,请将背景颜色设置为 clearColor 以外的其他颜色,例如 [UIColor redColor]。如果您在表格底部没有看到一些红条,则说明您的页脚没有设置。

    【讨论】:

    • 您也可以在 IB 中执行此操作。只需在 TableView 底部拖一个 UIView 并将高度设置为 1。
    【解决方案2】:

    我使用表格视图来显示固定数量的固定高度行,所以我只是调整了它的大小并使其不可滚动。

    【讨论】:

      【解决方案3】:

      界面构建器(iOS 9+)

      只需将 UIView 拖到表格中即可。在情节提要中,它将位于自定义单元格下方的顶部。您可能更愿意将其命名为“页脚”。

      为了清楚起见,这里显示为绿色,您可能需要清晰的颜色。

      请注意,通过调整高度,您可以根据自己的喜好影响桌子“底部弹跳”的处理方式。 (高度为零通常很好)。


      以编程方式进行:

      斯威夫特

      override func viewDidLoad() {
          super.viewDidLoad()
          self.tableView.tableFooterView = UIView()
      }
      

      目标-C

      iOS 6.1+

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

      或者,如果您愿意,

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

      iOS 历史上:

      添加到表格视图控制器...

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

      如果有必要...

      - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
      {        
          return [UIView new];
      
          // If you are not using ARC:
          // return [[UIView new] autorelease];
      }
      

      【讨论】:

      • 我认为你的描述和你的代码是相反的。无论如何,这对我很有用。我只对分组表使用“真实”页脚,所以我在代码周围添加了 if (tableView.style != UITableViewStyleGrouped) { }。现在我所有的非分组表都失去了额外的单元格,而我的分组表不受影响。
      • 在我上面的注释之后,我将添加一个警告,如果您将任何条件放入 viewForFooterInSection 方法,请确保该方法返回 UIView 或 nil。如果你不小心返回了void,你的应用就会崩溃!
      • @Mathieu 这将起作用,因为如果表格有自定义页脚,它将不会创建“fantom”行来填充表格视图。因此,您将创建一个空视图用作页脚并将高度设置为接近 0(它不能为零,否则 iOS 不会渲染它)。通过这个简单的技巧,您将更改默认行为。
      • @j.Costa:您的解决方案在 uitableview 创建时只能工作一次。单击最后一个单元格后,您的解决方案不是更好。请改进你的答案。
      • 使用 tableFooterView 会在行中的最后一个单元格上留下一个分隔符,因为表格仍会预测该单元格下方的内容。使用历史方法将消除最后一个分隔符,整体看起来更好。
      【解决方案4】:

      我想延长 wkw 答案:

      只需添加高度为 0 的页脚即可。 (在 sdk 4.2、4.4.1 上测试)

      - (void) addFooter
      {
          UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
      
          [self.myTableView setTableFooterView:v];
      }
      

      甚至更简单 - 在您设置 tableview 的地方,添加以下行:

      //change height value if extra space is needed at the bottom.
      [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectMake(0,0,0,0)]];
      

      甚至更简单 - 简单地删除任何分隔符:

      [_tableView setTableFooterView:[UIView new]];
      

      再次感谢wkw :)

      【讨论】:

      • 这是正确的答案,因为在其他方法中最后一行仍然没有行分隔符
      【解决方案5】:

      只需在 tableViewCell 的位置 x0 y-1 处添加一个具有所需分隔符颜色作为背景颜色、100% 宽度、1px 高度的视图。确保 tableViewCell 不剪辑子视图,而 tableView 应该。

      因此,您只能在现有单元格之间获得绝对简单且有效的分隔符,而无需针对每个代码或 IB 进行任何破解。

      注意:在垂直顶部反弹时,会显示第一个分隔符,但这应该不是问题,因为它是默认的 iOS 行为。

      【讨论】:

        【解决方案6】:

        试试这个。它对我有用:

        - (void) viewDidLoad
        {
          [super viewDidLoad];
        
          // Without ARC
          //self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
        
          // With ARC, tried on Xcode 5
          self.tableView.tableFooterView = [UIView new];
        }
        

        【讨论】:

          【解决方案7】:

          我知道这个问题已被接受,但我在这里提出了不同的方法来隐藏UITableView 的额外分隔线。

          您可以隐藏tableView 的标准分隔线,并在每个单元格的顶部添加您的自定义行。

          更新:

          添加自定义分隔符最简单的方法是添加简单的UIView 1px 高度:

          UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
          separatorLineView.backgroundColor = [UIColor grayColor]; /// may be here is clearColor;
          [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];
          }
          

          我喜欢的最好最简单的方法是

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

          尝试任何一种;

          【讨论】:

            【解决方案8】:

            对于 iOS 7+ 使用 Storyboards

            只需将UIView 拖入您的UITableView 作为页脚即可。将页脚视图的高度设置为 0。

            【讨论】:

              【解决方案9】:

              Swift

              中删除 UITableView 中空行的额外分隔线
              override func viewDidLoad() {
                  super.viewDidLoad()
                  // Do any additional setup after loading the view, typically from a nib.
                  self.yourTableview.tableFooterView = UIView()
              }
              

              【讨论】:

                【解决方案10】:

                推进 J. Costa 的解决方案:您可以通过输入这行代码来对表进行全局更改:

                [[UITableView appearance] setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
                

                在第一个可能的方法中(通常在AppDelegate,在:application:didFinishLaunchingWithOptions: 方法中)。

                【讨论】:

                • 小心这个,在某些情况下你的预期页脚可能会被覆盖,你会花一两天时间质疑你的理智。此外,tableFooterView 没有用 UI_APPEARANCE_SELECTOR 标记,因此实际行为可能随时改变。
                【解决方案11】:

                试试这个

                self.tables.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)];
                

                【讨论】:

                  【解决方案12】:

                  如果您使用的是 Swift,请将以下代码添加到管理 tableview 的控制器的 viewDidLoad 中:

                  override func viewDidLoad() {
                      super.viewDidLoad()
                  
                      //...
                  
                      // Remove extra separators
                      tableView.tableFooterView = UIView()
                  }
                  

                  【讨论】:

                    【解决方案13】:

                    对于斯威夫特:

                        override func viewDidLoad() {
                            super.viewDidLoad()
                            tableView.tableFooterView = UIView()
                        }
                    

                    【讨论】:

                      【解决方案14】:

                      如果你对 UITableView 进行子类化,你需要这样做......

                      -(void)didMoveToSuperview {
                          [super didMoveToSuperview];
                          self.tableFooterView = [UIView new];
                      }
                      

                      【讨论】:

                      • 每次将页脚移动到不同的超级视图时重新创建页脚是一种不好的做法。最好在init(frame:style:)+init?(coder:) 中执行相同的操作,以保证只执行一次。
                      【解决方案15】:

                      我很幸运地实现了一个已接受的答案(iOS 9+,Swift 2.2)。我曾尝试实施:

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

                      但是,我的 tableView 没有任何影响 - 我相信这可能与我使用 UITableViewController 的事实有关。

                      相反,我只需要重写viewForFooterInSection 方法(我没有在其他地方设置tableFooterView):

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

                      这适用于具有单个部分的tableView(如果您有多个部分,则需要指定最后一个部分)。

                      【讨论】:

                        【解决方案16】:

                        如果您的view 中有searchbar(例如限制结果数量),您还必须在shouldReloadTableForSearchStringshouldReloadTableForSearchScope: 中添加以下内容

                        controller.searchResultsTable.footerView = [ [ UIView alloc ] initWithFrame:CGRectZero ];
                        

                        【讨论】:

                          【解决方案17】:

                          uitableview 额外的分隔线隐藏额外的分隔线隐藏在 swift 3.0 中

                           self.tbltableView.tableFooterView = UIView(frame: .zero)
                          

                          【讨论】:

                            【解决方案18】:

                            Swift 适用于:

                            tableView.tableFooterView = UIView()
                            

                            【讨论】:

                              【解决方案19】:

                              我只是在 ViewDidLoad 函数中添加了这一行并修复了问题。

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

                              【讨论】:

                                【解决方案20】:

                                如果您不想在最后一个单元格之后使用任何分隔符,那么您的页脚需要一个接近零但非零的高度。

                                在你的UITableViewDelegate

                                func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
                                    return .leastNormalMagnitude
                                }
                                

                                【讨论】:

                                • 太棒了!为我工作!
                                【解决方案21】:

                                您可以在最后添加一个空页脚,然后它会隐藏空单元格,但它也会看起来很丑:

                                tableView.tableFooterView = UIView()
                                

                                有一个更好的方法:在表格视图的末尾添加一条 1 点线作为页脚,空单元格也将不再显示。

                                let footerView = UIView()
                                footerView.frame = CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1)
                                footerView.backgroundColor = tableView.separatorColor
                                tableView.tableFooterView = footerView
                                

                                【讨论】:

                                • 非常好的输入,但是不需要在最后一个单元格的底部使用厚分隔符颜色,只需使其与单元格的背景颜色相同,它也会消失。
                                • 随你喜欢。但我认为有点“未完成”的分隔符看起来很丑。
                                【解决方案22】:

                                如果你想删除 UITableview 中不需要的空间,你可以使用以下两种方法

                                - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
                                    return 0.1;
                                }
                                - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
                                {
                                    return 0.1;
                                }
                                

                                【讨论】:

                                  【解决方案23】:

                                  要以编程方式从 UItableview 底部删除多余的分隔线,只需写下以下两行代码,它将从中删除多余的分隔线。

                                  tableView.sectionFooterHeight = 0.f;
                                  tableView.sectionHeaderHeight = 0.f;
                                  

                                  这个技巧一直对我有用,你自己试试吧。

                                  【讨论】:

                                    【解决方案24】:

                                    只需添加此代码 (Swift) 。 .

                                    tableView.tableFooterView = UIView()
                                    

                                    【讨论】:

                                    【解决方案25】:

                                    如果您只有一个部分,那么最快和最简单的方法是将表格视图样式从“普通”设置为“分组”。 (见图)

                                    如果您有更多部分,您可能需要将标题高度设置为零(取决于您/您的客户/您的项目经理的品味)

                                    如果您有更多部分,并且不想弄乱标题(即使在最简单的情况下它只是一行),那么您需要将 UIView 设置为页脚,正如在以前的答案)

                                    【讨论】:

                                      【解决方案26】:

                                      Swift 4.0 扩展

                                      故事板的一个小扩展:

                                      extension UITableView {
                                          @IBInspectable
                                          var hideSeparatorForEmptyCells: Bool {
                                              set {
                                                  tableFooterView = newValue ? UIView() : nil
                                              }
                                              get {
                                                  return tableFooterView == nil
                                              }
                                          }
                                      }
                                      

                                      【讨论】:

                                        【解决方案27】:

                                        tableView 具有tableFooterView 时,UIKit 不会创建空单元格。所以我们可以制作一个技巧,并指定一个高度为零的UIView 对象作为tableView 的页脚。

                                        tableView.tableFooterView = UIView()
                                        

                                        【讨论】:

                                          【解决方案28】:

                                          快速简单的 Swift 4 方式。

                                          override func viewDidLoad() {
                                               tableView.tableFooterView = UIView(frame: .zero)
                                          }
                                          

                                          如果您有静态单元格。您还可以从 Inspector 窗口关闭分隔符。 (如果您需要分隔符,这将是不可取的。在这种情况下使用上面显示的方法)

                                          【讨论】:

                                          • Setting a new UIView to tableFooterView 已经回答了 10 次,对原因进行了更好的解释,更好的代码(您缺少 super.viewDidLoad())并且包含在顶部的 wiki 答案中。至于删除所有分隔符,这真的不是这个问题的目的,所以最好回滚你的最后一次编辑和/或完全删除你的答案。
                                          【解决方案29】:

                                          Swift 3 /Swift 4 /Swift 5 +,非常简单的方法

                                          override func viewWillAppear(_ animated: Bool) {
                                              super.viewWillAppear(animated)
                                                //MARK:- For Hiding the extra lines in table view.
                                              tableView?.tableFooterView = UIView()
                                          }
                                          

                                          override func viewDidLoad(_ animated: Bool) {
                                              super.viewDidLoad(animated)
                                                //MARK:- For Hiding the extra lines in table view.
                                              tableView?.tableFooterView = UIView()
                                          }
                                          

                                          【讨论】:

                                          • viewDidLoad(_ animated: Bool) 不存在,每次视图出现时设置页脚也没用。抱歉,您的回答完全错误,不会为现有答案增加任何价值。
                                          【解决方案30】:

                                          在 Swift(我使用的是 4.0)中,您可以通过创建自定义 UITableViewCell 类和 overriding setSelected 方法来完成此操作。然后将separator insets 全部设为0。 (我的带有表格视图的主类具有清晰的背景)颜色。

                                          override func setSelected(_ selected: Bool, animated: Bool) {
                                              super.setSelected(selected, animated: animated)
                                          
                                              // eliminate extra separators below UITableView
                                              self.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
                                          }
                                          

                                          【讨论】:

                                            猜你喜欢
                                            • 1970-01-01
                                            • 1970-01-01
                                            • 2019-10-23
                                            • 1970-01-01
                                            • 2012-01-11
                                            • 1970-01-01
                                            • 1970-01-01
                                            • 1970-01-01
                                            • 1970-01-01
                                            相关资源
                                            最近更新 更多