当我设置一个有 4 行的表格视图时,在填充的行下方仍然有额外的分隔线(或额外的空白单元格)。
我将如何删除这些单元格?
https://i.stack.imgur.com/cFbz5.png
界面生成器(iOS 9+)
只需将 UIView 拖到表格中即可。在情节提要中,它将位于自定义单元格下方的顶部。您可能更愿意将其命名为“页脚”。
为了清楚起见,这里以绿色显示,您可能想要清晰的颜色。
请注意,通过调整高度,您可以根据自己的喜好影响桌子“底部弹跳”的处理方式。 (高度为零通常很好)。
https://i.stack.imgur.com/OOwGA.png
以编程方式执行此操作:
迅速
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.tableFooterView = UIView()
}
Objective-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];
}
这是另一种不使用分组表格样式的方法,您可能不会猜到。在表格中添加页眉和页脚(可能一个或另一个就足够了,尚未检查)会导致分隔符从填充/空白行中消失。
我偶然发现了这一点,因为我希望在表格的顶部和底部留出一点空间,以减少点击按钮的风险,而不是手指多肉的表格单元格。这是一种将空白视图作为页眉和页脚粘贴的方法。使用您喜欢的任何高度,您仍然可以消除额外的分隔线。
- (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]
。如果您在表格底部没有看到一些红色条,则说明您的页脚未设置。
在 Swift 中删除 UITableView 中空行的额外分隔线
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.yourTableview.tableFooterView = UIView()
}
我想扩展 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 :)
对于使用故事板的 iOS 7+
只需将 UIView
拖入您的 UITableView
作为页脚。将页脚视图的高度设置为 0。
尝试这个。它对我有用:
- (void) viewDidLoad
{
[super viewDidLoad];
// Without ARC
//self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
// With ARC, tried on Xcode 5
self.tableView.tableFooterView = [UIView new];
}
如果您使用的是 Swift,请将以下代码添加到管理 tableview 的控制器的 viewDidLoad 中:
override func viewDidLoad() {
super.viewDidLoad()
//...
// Remove extra separators
tableView.tableFooterView = UIView()
}
对于斯威夫特:
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
}
您可以在最后添加一个空页脚,然后它将隐藏空单元格,但它看起来也很丑:
tableView.tableFooterView = UIView()
https://i.stack.imgur.com/Uq7fe.png
有一个更好的方法:在表格视图的末尾添加一条 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
https://i.stack.imgur.com/F0BnC.png
推进 J. Costa 的解决方案:您可以通过输入这行代码对表进行全局更改:
[[UITableView appearance] setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
在第一个可能的方法中(通常在 AppDelegate
中,在:application:didFinishLaunchingWithOptions:
方法中)。
Swift 适用于:
tableView.tableFooterView = UIView()
我知道这个问题已被接受,但我在这里提出了不同的方法来隐藏 UITableView
的额外分隔线。
您可以隐藏 tableView
的标准分隔线,并在每个单元格的顶部添加您的自定义线。
更新:
添加自定义分隔符的最简单方法是添加 1px 高度的简单 UIView
:
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];
尝试任何一种;
你可能会找到很多这个问题的答案。它们中的大多数都使用 UITableView
的 tableFooterView
属性进行操作,这是隐藏空行的正确方法。为了方便起见,我创建了简单的扩展,允许从 Interface Builder 打开/关闭空行。您可以从此 gist 文件中查看它。我希望它可以节省您的一点时间。
extension UITableView {
@IBInspectable
var isEmptyRowsHidden: Bool {
get {
return tableFooterView != nil
}
set {
if newValue {
tableFooterView = UIView(frame: .zero)
} else {
tableFooterView = nil
}
}
}
}
用法:
tableView.isEmptyRowsHidden = true
https://i.stack.imgur.com/vZpml.png
uitableview 额外的分隔线隐藏额外的分隔线隐藏在 swift 3.0
self.tbltableView.tableFooterView = UIView(frame: .zero)
如果您不想在最后一个单元格之后使用任何分隔符,那么您的页脚需要一个接近零但非零的高度。
在您的 UITableViewDelegate
中:
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return .leastNormalMagnitude
}
只需在 tableViewCell 的位置 x0 y-1 处添加一个具有所需分隔符颜色作为背景颜色、100% 宽度、1px 高度的视图。确保 tableViewCell 不剪辑子视图,而 tableView 应该。
因此,您只能在现有单元之间获得一个绝对简单且有效的分隔符,而无需对每个代码或 IB 进行任何破解。
注意:在垂直顶部反弹时,第一个分隔符会出现,但这应该不是问题,因为它是默认的 iOS 行为。
我使用表格视图来显示固定数量的固定高度行,所以我只是调整了它的大小并使其不可滚动。
要以编程方式从 UItableview 底部删除额外的分隔线,只需写下以下两行代码,它将从中删除额外的分隔线。
tableView.sectionFooterHeight = 0.f;
tableView.sectionHeaderHeight = 0.f;
这个技巧一直对我有用,你自己试试。
我很幸运地实现了一个已接受的答案(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
(如果您有多个部分,则需要指定最后一个)。
如果您只有一个部分,那么最快和最简单的方法是将表格视图样式从“普通”设置为“分组”。 (见图)
https://i.stack.imgur.com/yGCuB.png
如果您有更多部分,您可能需要将标题高度设置为零(取决于您/您的客户/您的项目经理的品味)
如果您有更多部分,并且不想弄乱标题(即使在最简单的情况下只是一行),那么您需要将 UIView 设置为页脚,正如前面的答案中所解释的那样)
Swift 4.0 扩展
只是故事板的一个小扩展:
https://i.stack.imgur.com/7wks9.png
extension UITableView {
@IBInspectable
var hideSeparatorForEmptyCells: Bool {
set {
tableFooterView = newValue ? UIView() : nil
}
get {
return tableFooterView == nil
}
}
}
快速简单的 Swift 4 方式。
override func viewDidLoad() {
tableView.tableFooterView = UIView(frame: .zero)
}
https://i.stack.imgur.com/BvNg6.png
super.viewDidLoad()
)并且包含在顶部的 wiki 答案中。至于删除所有分隔符,这真的不是这个问题的目的,所以最好回滚你的最后一次编辑和/或完全删除你的答案。
试试这个
对于目标 C
- (void)viewDidLoad
{
[super viewDidLoad];
// This will remove extra separators from tableview
self.yourTableView.tableFooterView = [UIView new];
}
斯威夫特
override func viewDidLoad() {
super.viewDidLoad()
self.yourTableView.tableFooterView = UIView()
}
如果要删除 UITableview 中不需要的空间,可以使用以下两种方法
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.1;
}
我添加了这个小表格视图扩展,它在整个过程中都有帮助
extension UITableView {
func removeExtraCells() {
tableFooterView = UIView(frame: .zero)
}
}
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)
不存在,每次视图出现时设置页脚也没用。抱歉,您的回答完全错误,不会为现有答案增加任何价值。
尝试这个
self.tables.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)];
当 tableView
有 tableFooterView
时,UIKit
不会创建空单元格。所以我们可以制作一个技巧,并指定一个高度为零的 UIView
对象作为 tableView
的页脚。
tableView.tableFooterView = UIView()
如果您的 view
中有 searchbar
(例如限制结果数量),您还必须在 shouldReloadTableForSearchString
和 shouldReloadTableForSearchScope:
中添加以下内容
controller.searchResultsTable.footerView = [ [ UIView alloc ] initWithFrame:CGRectZero ];
tableFooterView
在行中的最后一个单元格上留下一个分隔符,因为表格仍然预期该单元格下方的内容。使用历史方法将消除最后一个分隔符,整体看起来更好。