ios 如何删除 UITableView 中的空单元格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14520185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove empty cells in UITableView?
提问by z22
i am trying to display a simple UITableView
with some data. I wish to set the static height of the UITableView
so that it doesn't displays empty cells at the end of the table. how do I do that?
我试图UITableView
用一些数据显示一个简单的。我希望设置 的静态高度,UITableView
以便它不会在表格末尾显示空单元格。我怎么做?
code:
代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"%d", [arr count]);
return [arr count];
}
回答by Andy
Set a zero height table footer view (perhaps in your viewDidLoad
method), like so:
设置零高度表页脚视图(可能在您的viewDidLoad
方法中),如下所示:
Swift:
迅速:
tableView.tableFooterView = UIView()
Objective-C:
目标-C:
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Because the table thinks there is a footer to show, it doesn't display any cells beyond those you explicitly asked for.
因为表格认为有一个页脚要显示,所以除了您明确要求的单元格之外,它不会显示任何单元格。
Interface builder pro-tip:
界面构建器专业提示:
If you are using a xib/Storyboard, you can just drag a UIView (with height 0pt) onto the bottom of the UITableView.
如果您使用的是xib/Storyboard,则只需将 UIView(高度为 0pt)拖到 UITableView 的底部即可。
回答by Dustin Williams
Swift 3 syntax:
Swift 3 语法:
tableView.tableFooterView = UIView(frame: .zero)
Swift syntax: < 2.0
Swift 语法:< 2.0
tableView.tableFooterView = UIView(frame: CGRect.zeroRect)
Swift 2.0 syntax:
Swift 2.0 语法:
tableView.tableFooterView = UIView(frame: CGRect.zero)
回答by Gabriel Diaconescu
In the Storyboard, select the UITableView
, and modify the property Style from Plain
to Grouped
.
在 Storyboard 中,选择UITableView
,并将属性 Style 从 修改Plain
为Grouped
。
回答by Leonardo Jorge
Implemented with swift on Xcode 6.1
在 Xcode 6.1 上用 swift 实现
self.tableView.tableFooterView = UIView(frame: CGRectZero)
self.tableView.tableFooterView?.hidden = true
The second line of code does not cause any effect on presentation, you can use to check if is hidden or not.
第二行代码对展示没有任何影响,可以用来检查是否隐藏。
Answer taken from this link Fail to hide empty cells in UITableView Swift
从此链接中获取的答案 无法隐藏 UITableView Swift 中的空单元格
回答by Sahil
I can not add comment as of now so adding this as an answer.
到目前为止,我无法添加评论,因此将其添加为答案。
@Andy's answer is good and the same results can be achieved with the following line of code:
@Andy 的回答很好,使用以下代码行可以获得相同的结果:
tableView.tableFooterView = [UIView new];
'new' method belongs to NSObject
class and invokes alloc
and init
methods for UIView
.
“新”方法属于NSObject
类并调用alloc
和init
方法UIView
。
回答by Filozof Abi
I tried the code:
我试过代码:
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
In the viewDidLoadsection and xcode6 showed a warning. I have put a "self." in front of it and now it works fine. so the working code I use is:
在viewDidLoad部分和 xcode6 显示警告。我已经放了一个“自我”。在它面前,现在它工作正常。所以我使用的工作代码是:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
回答by KostiaZzz
or you can call tableView method to set the footer height in 1 point, and it will add an last line, but you can hide it too, by setting footer background color.
或者您可以调用 tableView 方法将页脚高度设置为 1 点,它会添加最后一行,但您也可以通过设置页脚背景颜色来隐藏它。
code:
代码:
func tableView(tableView: UITableView,heightForFooterInSection section: Int) -> CGFloat {
return 1
}
回答by A.G
override func viewWillAppear(animated: Bool) {
self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect)
/// OR
self.tableView.tableFooterView = UIView()
}
回答by Manan Devani
Using UITableViewController
使用 UITableViewController
The solution accepted will change the height of the TableViewCell
. To fix that, perform following steps:
接受的解决方案将改变TableViewCell
. 要解决该问题,请执行以下步骤:
Write code snippet given below in
ViewDidLoad
method.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Add following method in the
TableViewClass.m
file.- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return (cell height set on storyboard); }
在
ViewDidLoad
方法中编写下面给出的代码片段。tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
在
TableViewClass.m
文件中添加以下方法。- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return (cell height set on storyboard); }
That's it. You can build and run your project.
就是这样。您可以构建和运行您的项目。
回答by Bharath Raj
in the below method:
在下面的方法中:
- (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];
}
here 65 is the height of the cell and 66 is the height of the navigation bar in UIViewController.
这里65是单元格的高度,66是UIViewController中导航栏的高度。