【问题标题】:How to remove empty cells in UITableView? [duplicate]如何删除 UITableView 中的空单元格? [复制]
【发布时间】:2013-01-09 07:42:31
【问题描述】:

我试图用一些数据显示一个简单的UITableView。我希望设置UITableView 的静态高度,以便它不会在表格末尾显示空单元格。我该怎么做?

代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"%d", [arr count]);
    return [arr count];
}

【问题讨论】:

  • 正确设置 numberOfRows 然后它不会显示空单元格

标签: ios objective-c uitableview


【解决方案1】:

我现在无法添加评论,所以添加这个作为答案。

@Andy 的回答很好,用下面这行代码也可以达到同样的效果:

tableView.tableFooterView = [UIView new];

'new' 方法属于NSObject 类,并为UIView 调用allocinit 方法。

【讨论】:

  • 这行得通,但在技术上与安迪的回答不同。使用 new 只是调用“init”方法,但不能保证 [myView init] 和 [myView initWithFrame:CGRectZero] 是相同的,即使在今天的实现中,它们看起来是一样的。
【解决方案2】:

设置一个零高度的表格页脚视图(可能在您的viewDidLoad 方法中),如下所示:

斯威夫特:

tableView.tableFooterView = UIView()

目标-C:

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

因为表格认为有一个页脚要显示,所以它不会显示超出您明确要求的单元格。

界面构建器专业提示:

如果您使用的是 xib/Storyboard,您只需将 UIView(高度为 0pt)拖到 UITableView 的底部即可。

【讨论】:

  • 那行代码将以什么方式添加? tableView willDisplayFooterView??
  • @Jupiter869 我只是把它放在 ViewDidLoad 中就可以了
  • SWIFT 2.0 : self.tableView.tableFooterView = UIView(frame: CGRectZero)
  • 将高度为 0 的视图添加到情节提要中任何单元格下方的表格视图中效果很好。
  • 太棒了。为我节省了大量时间。谢谢亲
【解决方案3】:

Swift 3 语法:

tableView.tableFooterView = UIView(frame: .zero)

Swift 语法:

tableView.tableFooterView = UIView(frame: CGRect.zeroRect)

Swift 2.0 语法:

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

【讨论】:

  • 现在是 tableView.tableFooterView = UIView(frame: CGRect.zero)
  • 用 ios 8+ 测试并且工作正常。
  • 以防万一有人感兴趣,Swift 实际上让你更简洁:tableView.tableFooterView = UIView(frame: .zero)
  • 更短! tableView.tablFooterView = UIView()
【解决方案4】:

在下面的方法中:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (([array count]*65) > [UIScreen mainScreen].bounds.size.height - 66)
    {
        Table.frame = CGRectMake(0, 66, self.view.frame.size.width, [array count]*65)); 
    }
    else
    {
        Table.frame = CGRectMake(0, 66, self.view.frame.size.width, [UIScreen mainScreen].bounds.size.height - 66);
    }
    return [array count];
}

这里 65 是单元格的高度,66 是 UIViewController 中导航栏的高度。

【讨论】:

    【解决方案5】:

    或者你可以调用tableView方法将页脚高度设置为1点,它会添加最后一行,但你也可以通过设置页脚背景颜色来隐藏它。

    代码:

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

    looks like last line

    【讨论】:

      【解决方案6】:
      override func viewWillAppear(animated: Bool) {
          self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect)
      
      /// OR 
      
          self.tableView.tableFooterView = UIView()
      }
      

      【讨论】:

        【解决方案7】:

        使用UITableViewController

        接受的解决方案将改变TableViewCell 的高度。要解决此问题,请执行以下步骤:

        1. ViewDidLoad方法中编写下面给出的代码sn-p。

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

        2. TableViewClass.m文件中添加如下方法。

          - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
          {
              return (cell height set on storyboard); 
          }
          

        就是这样。您可以构建并运行您的项目。

        【讨论】:

          【解决方案8】:

          我试过代码:

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

          viewDidLoad 部分和 xcode6 显示警告。我放了一个“自我”。在它前面,现在它工作正常。所以我使用的工作代码是:

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

          【讨论】:

            【解决方案9】:

            在 Xcode 6.1 上使用 swift 实现

            self.tableView.tableFooterView = UIView(frame: CGRectZero)
            self.tableView.tableFooterView?.hidden = true
            

            第二行代码对展示没有任何影响,可以用来检查是否隐藏。

            来自此链接的答案 Fail to hide empty cells in UITableView Swift

            【讨论】:

            • 这是使用 Swift 和 XCode 6.1 为我完成的!已尝试上述答案的 Swift 等效版本,但没有成功。
            【解决方案10】:

            在情节提要中,选择UITableView,并将属性样式从Plain 修改为Grouped

            【讨论】:

            • 最佳和最直接的答案 1
            • 但它在表格部分上方和下方增加了额外的间距。
            • 也可以在代码中使用。
            猜你喜欢
            • 2017-06-17
            • 1970-01-01
            • 2013-09-25
            • 1970-01-01
            • 1970-01-01
            • 2021-11-18
            • 1970-01-01
            相关资源
            最近更新 更多