当我设置一个有4行的表格视图时,在填充行的下面仍然有额外的分隔行(或额外的空白单元格)。

如何移除这些单元格?


当前回答

迅速:

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()
    }

其他回答

Uitableview额外的分隔线隐藏额外的分隔线隐藏在swift 3.0

 self.tbltableView.tableFooterView = UIView(frame: .zero)

改进J. Costa的解决方案:你可以通过下面这行代码对表进行全局更改:

[[UITableView appearance] setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];

在第一个可能的方法中(通常在AppDelegate中,在:application:didFinishLaunchingWithOptions: method)。

Swift 4.0扩展

只是一个小扩展的故事板:

extension UITableView {
    @IBInspectable
    var hideSeparatorForEmptyCells: Bool {
        set {
            tableFooterView = newValue ? UIView() : nil
        }
        get {
            return tableFooterView == nil
        }
    }
}

我添加了这个小的tableview扩展,有助于整个

extension UITableView {
     func removeExtraCells() {
         tableFooterView = UIView(frame: .zero)
     }
}

你可以在最后添加一个空的页脚,它会隐藏空的单元格,但它也会看起来很难看:

tableView.tableFooterView = UIView()

有一个更好的方法:在表格视图的末尾添加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