如何删除UITableView中的空单元格? [重复]

戚翰飞
2023-12-01

本文翻译自:How to remove empty cells in UITableView? [duplicate]

This question already has an answer here: 这个问题已经在这里有了答案:

i am trying to display a simple UITableView with some data. 我正在尝试显示一些数据的简单UITableView I wish to set the static height of the UITableView so that it doesn't displays empty cells at the end of the table. 我希望设置UITableView的静态高度,以使其在表的末尾不显示空单元格。 how do I do that? 我怎么做?

code: 码:

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

#1楼

参考:https://stackoom.com/question/yvMX/如何删除UITableView中的空单元格-重复


#2楼

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的底部即可。


#3楼

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 . “ new”方法属于NSObject类,并为UIView调用allocinit方法。


#4楼

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


#5楼

Implemented with swift on Xcode 6.1 在Xcode 6.1上快速实现

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中隐藏空单元格


#6楼

I tried the code: 我尝试了代码:

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

In the viewDidLoad section and xcode6 showed a warning. viewDidLoad部分和xcode6中显示了警告。 I have put a "self." 我提出了一个“自我”。 in front of it and now it works fine. 在它前面,现在工作正常。 so the working code I use is: 所以我使用的工作代码是:

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
 类似资料: