如何删除UITableView中的空单元格?

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

码:

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

设置一个零viewDidLoad脚注视图(也许在你的viewDidLoad方法),如下所示:

迅速:

 tableView.tableFooterView = UIView() 

Objective-C的:

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

因为表格认为有一个页脚可以显示,所以不会显示任何你明确要求的单元格。

界面生成器pro-tip:

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

Swift 3语法:

 tableView.tableFooterView = UIView(frame: .zero) 

Swift语法:<2.0

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

Swift 2.0语法:

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

在故事板中,selectUITableView ,然后将属性Style从Plain修改为Grouped

在Xcode 6.1上用swift实现

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

第二行代码不会对演示产生任何影响,您可以使用来检查是否隐藏。

从这个链接采取的答案无法隐藏UITableView Swift中的空单元格

我现在不能添加评论,所以join这个答案。

@ Andy的回答很好,用下面这行代码可以得到相同的结果:

 tableView.tableFooterView = [UIView new]; 

'new'方法属于NSObject类,调用UIView的alloc和init方法。

我试了一下代码:

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

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

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

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

码:

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

看起来像最后一行

使用UITableViewController

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

  1. ViewDidLoad方法中写下面给出的代码片段。

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

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

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

而已。 您可以构build并运行您的项目。

 override func viewWillAppear(animated: Bool) { self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect) /// OR self.tableView.tableFooterView = UIView() } 

在下面的方法中:

 - (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中导航栏的高度。

在viewDidLoad中:

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