当我设置一个有4行的表格视图时,在填充行的下面仍然有额外的分隔行(或额外的空白单元格)。
如何移除这些单元格?
当我设置一个有4行的表格视图时,在填充行的下面仍然有额外的分隔行(或额外的空白单元格)。
如何移除这些单元格?
当前回答
快捷便捷的Swift 4方式。
override func viewDidLoad() {
tableView.tableFooterView = UIView(frame: .zero)
}
如果你有静态单元格。您也可以从检查器窗口关闭分隔符。(如果你需要分隔符,这是不可取的。在这种情况下,使用上述方法)
其他回答
你可能会找到这个问题的很多答案。大多数都围绕着UITableView的tableFooterView属性进行操作这是隐藏空行的合适方法。为了方便起见,我创建了一个简单的扩展,它允许打开/关闭接口生成器中的空行。 你可以从这个要点文件里查到。我希望它能为你节省一点时间。
extension UITableView {
@IBInspectable
var isEmptyRowsHidden: Bool {
get {
return tableFooterView != nil
}
set {
if newValue {
tableFooterView = UIView(frame: .zero)
} else {
tableFooterView = nil
}
}
}
}
用法:
tableView.isEmptyRowsHidden = true
界面构建器(iOS 9+)
只需将一个UIView拖到表中。在故事板中,它将位于自定义单元格下方的顶部。您可能更喜欢将其命名为“footer”。
为了清晰起见,这里显示为绿色,你可能想要清晰的颜色。
请注意,通过调整高度,您可以根据自己的喜好影响如何处理表的“底部反弹”。(高度通常为0)。
用程序来做:
斯威夫特
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];
}
在Swift中(我使用4.0),你可以通过创建一个自定义UITableViewCell类来实现这一点,并覆盖setSelected方法。然后分隔符将全部插入到0。(我的主类与表视图有一个清晰的背景)颜色。
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// eliminate extra separators below UITableView
self.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
我使用一个表视图来显示固定数量的固定高度行,所以我简单地调整了它的大小并使其不可滚动。
改进J. Costa的解决方案:你可以通过下面这行代码对表进行全局更改:
[[UITableView appearance] setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
在第一个可能的方法中(通常在AppDelegate中,在:application:didFinishLaunchingWithOptions: method)。