如何删除 TableViewController 中多余的空单元格,iOS - Swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28708574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:58:20  来源:igfitidea点击:

How to remove extra empty cells in TableViewController, iOS - Swift

iosswifttableview

提问by Stephen Fox

Hi I have a TableViewControllerwith two static cells, however when displayed, it shows the two static cells, and then all the other empty cells for the tableview. However I don't want to display those cells as they are useless. How do I hide the rest of the cells apart from the two static cell?

嗨,我有TableViewController两个静态单元格,但是在显示时,它显示了两个静态单元格,然后是 tableview 的所有其他空单元格。但是我不想显示这些单元格,因为它们没用。除了两个静态单元格之外,如何隐藏其余单元格?

回答by HorseT

in your viewDidLoad()function add this line of code:

在您的viewDidLoad()函数中添加这行代码:

tableView.tableFooterView = UIView()

回答by bakalolo

Select your UITableView, go to the attribute inspector and change the style to Grouped.

选择您的 UITableView,转到属性检查器并将样式更改为 Grouped。

enter image description here

在此处输入图片说明

回答by toast

To add to this, the reason why setting tableFooterViewto UIView()removes the rows is because you are setting an empty UIView()object to the property. As per the Apple documentation:

要添加到这一点,为什么设置的原因tableFooterView,以UIView()消除此行是因为你设置一个空的UIView()对象属性。根据苹果文档:

var tableFooterView: UIView?

Therefore setting an empty UIView() will display nothing below your data for this property.

因此,设置一个空的 UIView() 将不会在此属性的数据下方显示任何内容。

You can bring the empty rows back by resetting tableFooterViewto the default value, like so:

您可以通过重置tableFooterView为默认值来恢复空行,如下所示:

tableView.tableFooterView = nil

回答by Giang

Setting the tableFooterViewshould remove extra cell like views.

设置tableFooterView应该删除额外的单元格,如视图。

   @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.tableFooterView = UIView(frame: .zero)
        }
    }

回答by quickthyme

You can achieve this also in Storyboard by simply dragging a view into the table view's footer area, and then set its height and background color to 1 and clear respectively using the property inspector.

您也可以在 Storyboard 中实现这一点,只需将一个视图拖入表格视图的页脚区域,然后使用属性检查器将其高度和背景颜色分别设置为 1 和清除。

(This assumes that you either can't or don't want to just use the Group style setting for some reason. Otherwise, just do that.)

(这假设您由于某种原因不能或不想只使用组样式设置。否则,就这样做。)

回答by elarctheitroadis

Swift 4Elaborating on HorseT's answer, first control drag from your Table View in your storyboard, to your View Controller class and create an outlet called tableView.

Swift 4详细说明 HorseT 的答案,首先控制从故事板中的 Table View 拖动到 View Controller 类并创建一个名为 tableView 的插座。

@IBOutlet weak var tableView: UITableView!

Then, your viewDidLoad in your View Controller should look something like this:

然后,您的视图控制器中的 viewDidLoad 应如下所示:

    override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = self
    tableView.delegate = self

    tableView.tableFooterView = UIView()
}

回答by Rizwan Mehboob

Based on above answers in swift.

基于swift 中的上述答案。

tableView.tableFooterView = UIView(frame: .zero)

回答by Mannopson

tableView.tableFooterView = UIView.init(frame: CGRect.zero)

Call this code inside of the viewWillAppearmethod. And will work

viewWillAppear方法内部调用此代码。并且会工作

回答by Dhaval Gevariya

Add Below Code in your "override func viewDidLoad()" Or "override func viewWillAppear(_ animated: Bool)"functions.

在您的“ override func viewDidLoad()”或“override func viewWillAppear(_ animation: Bool)”函数中添加以下代码。

tblOutletName.tableFooterView = UIView()

tblOutletName.tableFooterView = UIView()

回答by mkhoshpour

if you want to remove rows for all UITableViewsin your app, just add an extension like this:

如果您想删除UITableViews应用程序中的所有行,只需添加如下扩展名:

Swift 4.0

斯威夫特 4.0

extension UITableView {

override open func didMoveToSuperview() {
    super.didMoveToSuperview()
    self.tableFooterView = UIView()

}