如何以编程方式隐藏或删除表视图中的额外单元格和分隔符 swift 2.2

how to programmatically hide or delete extra cell and seperators in tableview swift 2.2

在我的应用程序中,我有一个包含 5 个单元格数据的 UITableView。什么时候我会 运行 这个应用程序。它将在输出中正确显示 5 个包含数据的单元格。但是有很多额外的单元格显示为空。我想删除 UITableView 中的多余单元格。我也在 objective-C 中完成了,但我在 Swift 2.2 中需要一些帮助,而且我是 swift 中的新手。

在您的 viewDidLoad() 中添加此行:

yourtableView.tableFooterView = UIView()

或使用

 yourtableView.tableFooterView = UIView(frame: CGRectZero)

像这样实现这个委托方法:

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }

问题可能是因为您在 UITableView 中指定的行数多于填充数据所需的行数。假设您从数组中填充 UITableView,您应该将行计数设置为数组的计数。

如果您正在使用 UITableViewController 覆盖此方法

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourArray.count

}

如果 UITableView 嵌入到 UIViewController 中,请将 UITableView 委托给自己并实现此方法。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return yourArray.count
}

试试下面的代码。也许这对你有帮助。

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

写这段代码

self.tableView.tableFooterView = UIView(frame: CGRectZero)