如何删除 TableViewController 中多余的空单元格,iOS - Swift
How to remove extra empty cells in TableViewController, iOS - Swift
您好,我有一个带有两个静态单元格的 TableViewController
,但是在显示时,它显示了两个静态单元格,然后是表格视图的所有其他空单元格。但是我不想显示这些单元格,因为它们没用。如何隐藏除两个静态单元格之外的其余单元格?
在您的 viewDidLoad()
函数中添加这行代码:
tableView.tableFooterView = UIView()
此外,将 tableFooterView
设置为 UIView()
会删除行的原因是您将空 UIView()
对象设置为 属性。根据 Apple 文档:
var tableFooterView: UIView?
因此,设置一个空的 UIView() 将不会在您的数据下方显示任何内容 属性。
您可以通过将 tableFooterView
重置为默认值来恢复空行,如下所示:
tableView.tableFooterView = nil
您也可以在 Storyboard 中实现这一点,只需将视图拖到 table 视图的页脚区域,然后使用 属性 检查器分别将其高度和背景色设置为 1 和清除。
(假设您出于某种原因不能或不想只使用组样式设置。否则,就那样做。)
tableView.tableFooterView = UIView.init(frame: CGRect.zero)
在 viewDidLoad
方法中调用此代码。
只需添加:
tableView.tableFooterView = UIView()
说明:
这是完全有效的,并且在 swift 3.By 中完美运行,因为您将一个空的 UIView() 对象设置为 属性,将 tableFooterView 设置为 UIView() 会删除行.
Select 您的 UITableView,转到属性检查器并将样式更改为分组。
我尝试了 tableview.tableFooterView = UIView() 但它对我不起作用。我正在使用自定义 TableViewViewCell,也许这就是原因。
不过我确实找到了一个 hack。在我的 func numberOfRowsInSection
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourArrayClass.count - 1
}
这使得我额外的空白自定义 UITableViewCell 不再显示。当然是额外的空白。我将通过添加更多单元格对其进行更多测试。作为参考,我在 cellForRowAt 函数中使用 Swift 3, Firebase,一个自定义单元格 class。
Swift 4
详细说明 HorseT 的回答,首先控制从故事板中的 Table 视图拖动到视图控制器 class 并创建一个名为 tableView 的出口。
@IBOutlet weak var tableView: UITableView!
然后,View Controller 中的 viewDidLoad 应该如下所示:
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView()
}
基于 swift.
中的上述答案
tableView.tableFooterView = UIView(frame: .zero)
以上回答正确。我们可以通过添加
来实现
tableView.tableFooterView = UIView() // to remove extra cells in tableView
如果您想删除应用中所有 UITableViews
的行,只需添加如下扩展名:
Swift 5.0
extension UITableView {
override open func didMoveToSuperview() {
super.didMoveToSuperview()
self.tableFooterView = UIView()
}
设置 tableFooterView
应该会删除多余的单元格,例如视图。
@IBOutlet weak var tableView: UITableView! {
didSet {
tableView.tableFooterView = UIView(frame: .zero)
}
}
在您的“override func viewDidLoad()”或"override func viewWillAppear(_ animated: Bool)" 函数中添加以下代码。
tblOutletName.tableFooterView = UIView()
您好,我有一个带有两个静态单元格的 TableViewController
,但是在显示时,它显示了两个静态单元格,然后是表格视图的所有其他空单元格。但是我不想显示这些单元格,因为它们没用。如何隐藏除两个静态单元格之外的其余单元格?
在您的 viewDidLoad()
函数中添加这行代码:
tableView.tableFooterView = UIView()
此外,将 tableFooterView
设置为 UIView()
会删除行的原因是您将空 UIView()
对象设置为 属性。根据 Apple 文档:
var tableFooterView: UIView?
因此,设置一个空的 UIView() 将不会在您的数据下方显示任何内容 属性。
您可以通过将 tableFooterView
重置为默认值来恢复空行,如下所示:
tableView.tableFooterView = nil
您也可以在 Storyboard 中实现这一点,只需将视图拖到 table 视图的页脚区域,然后使用 属性 检查器分别将其高度和背景色设置为 1 和清除。
(假设您出于某种原因不能或不想只使用组样式设置。否则,就那样做。)
tableView.tableFooterView = UIView.init(frame: CGRect.zero)
在 viewDidLoad
方法中调用此代码。
只需添加:
tableView.tableFooterView = UIView()
说明:
这是完全有效的,并且在 swift 3.By 中完美运行,因为您将一个空的 UIView() 对象设置为 属性,将 tableFooterView 设置为 UIView() 会删除行.
Select 您的 UITableView,转到属性检查器并将样式更改为分组。
我尝试了 tableview.tableFooterView = UIView() 但它对我不起作用。我正在使用自定义 TableViewViewCell,也许这就是原因。
不过我确实找到了一个 hack。在我的 func numberOfRowsInSection
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourArrayClass.count - 1
}
这使得我额外的空白自定义 UITableViewCell 不再显示。当然是额外的空白。我将通过添加更多单元格对其进行更多测试。作为参考,我在 cellForRowAt 函数中使用 Swift 3, Firebase,一个自定义单元格 class。
Swift 4 详细说明 HorseT 的回答,首先控制从故事板中的 Table 视图拖动到视图控制器 class 并创建一个名为 tableView 的出口。
@IBOutlet weak var tableView: UITableView!
然后,View Controller 中的 viewDidLoad 应该如下所示:
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView()
}
基于 swift.
中的上述答案tableView.tableFooterView = UIView(frame: .zero)
以上回答正确。我们可以通过添加
来实现tableView.tableFooterView = UIView() // to remove extra cells in tableView
如果您想删除应用中所有 UITableViews
的行,只需添加如下扩展名:
Swift 5.0
extension UITableView {
override open func didMoveToSuperview() {
super.didMoveToSuperview()
self.tableFooterView = UIView()
}
设置 tableFooterView
应该会删除多余的单元格,例如视图。
@IBOutlet weak var tableView: UITableView! {
didSet {
tableView.tableFooterView = UIView(frame: .zero)
}
}
在您的“override func viewDidLoad()”或"override func viewWillAppear(_ animated: Bool)" 函数中添加以下代码。
tblOutletName.tableFooterView = UIView()