简体   繁体   English

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

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

Hi I have a TableViewController with two static cells, however when displayed, it shows the two static cells, and then all the other empty cells for the tableview.嗨,我有一个带有两个静态单元格的TableViewController ,但是在显示时,它显示了两个静态单元格,然后是 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?除了两个静态单元格之外,如何隐藏其余单元格?

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

tableView.tableFooterView = UIView()

Select your UITableView, go to the attribute inspector and change the style to Grouped.选择您的 UITableView,转到属性检查器并将样式更改为 Grouped。

在此处输入图片说明

To add to this, the reason why setting tableFooterView to UIView() removes the rows is because you are setting an empty UIView() object to the property. tableFooterView ,将tableFooterView设置为UIView()删除行的原因是因为您正在为该属性设置一个空的UIView()对象。 As per the Apple documentation:根据苹果文档:

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 tableFooterView to the default value, like so:您可以通过将tableFooterView重置为默认值来恢复空行,如下所示:

tableView.tableFooterView = nil

Setting the tableFooterView should remove extra cell like views.设置tableFooterView应该删除像视图一样的额外单元格。

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

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.) (这假设您由于某种原因不能或不想只使用组样式设置。否则,就这样做。)

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

viewDidLoad方法中调用此代码。

Swift 4 Elaborating 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()
}

基于swift 中的上述答案。

tableView.tableFooterView = UIView(frame: .zero)

if you want to remove rows for all UITableViews in your app, just add an extension like this:如果您想删除应用程序中所有UITableViews行,只需添加如下扩展:

Swift 5.0斯威夫特 5.0

extension UITableView {

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

}

Simply add:只需添加:

tableView.tableFooterView = UIView()

Explanation:解释:

This is totally valid and works perfectly in swift 3.By Since you are setting an empty UIView() object to the property, Setting tableFooterView to UIView() removes the rows.这是完全有效的,并且在 swift 中完美运行 3.By 由于您将一个空的 UIView() 对象设置为该属性,因此将 tableFooterView 设置为 UIView() 会删除行。

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()

Abobe anwer is right. Abobe anwer 是对的。 We can achieve this by adding我们可以通过添加来实现这一点

tableView.tableFooterView = UIView() // to remove extra cells in tableView

I tried the tableview.tableFooterView = UIView() and it did not work for me.我尝试了 tableview.tableFooterView = UIView() 但它对我不起作用。 I am using a custom TableViewViewCell, perhaps that is why.我正在使用自定义 TableViewViewCell,也许这就是原因。

I did find a hack tho.我确实找到了一个黑客。 In my func numberOfRowsInSection在我的函数 numberOfRowsInSection

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

And that makes my extra blank custom UITableViewCell not display anymore.这使得我额外的空白自定义 UITableViewCell 不再显示。 Of course its the extra blank one.当然,它是额外的空白。 I will test it some more by adding more Cells.我将通过添加更多单元格来对其进行更多测试。 For reference, I am using Swift 3, Firebase, a custom cell class, inside the cellForRowAt function.作为参考,我在 cellForRowAt 函数中使用了 Swift 3、Firebase,这是一个自定义单元格类。

In swift 4 you can remove those unnecessary lines with a simple one line code: 在Swift 4中,您可以使用一个简单的一行代码删除那些不必要的行:

//tableview refers to your UITableView
tableView.separatorStyle = .none

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM