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

IosSwiftTableview

Ios Problem Overview


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. 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?

Ios Solutions


Solution 1 - Ios

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

tableView.tableFooterView = UIView()

Solution 2 - Ios

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

enter image description here

Solution 3 - Ios

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. As per the Apple documentation:

var tableFooterView: UIView?

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

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

tableView.tableFooterView = nil

Solution 4 - Ios

Setting the tableFooterView should remove extra cell like views.

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

Solution 5 - Ios

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

Call this code in viewDidLoad method.

Solution 6 - Ios

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.

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

Solution 7 - Ios

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.

@IBOutlet weak var tableView: UITableView!

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

    override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = self
    tableView.delegate = self
    
    tableView.tableFooterView = UIView()
}

Solution 8 - Ios

Based on above answers in swift.

tableView.tableFooterView = UIView(frame: .zero)

Solution 9 - Ios

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

Swift 5.0

extension UITableView {

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

}

Solution 10 - Ios

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.

Solution 11 - Ios

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

> tblOutletName.tableFooterView = UIView()

Solution 12 - Ios

Abobe anwer is right. We can achieve this by adding

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

Solution 13 - Ios

I tried the tableview.tableFooterView = UIView() and it did not work for me. I am using a custom TableViewViewCell, perhaps that is why.

I did find a hack tho. In my func numberOfRowsInSection

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

And that makes my extra blank custom UITableViewCell not display anymore. 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStephen FoxView Question on Stackoverflow
Solution 1 - IosHorseTView Answer on Stackoverflow
Solution 2 - IosbakaloloView Answer on Stackoverflow
Solution 3 - IostoastView Answer on Stackoverflow
Solution 4 - IosGiangView Answer on Stackoverflow
Solution 5 - IosMannopsonView Answer on Stackoverflow
Solution 6 - IosquickthymeView Answer on Stackoverflow
Solution 7 - IoselarcoirisView Answer on Stackoverflow
Solution 8 - IosRizwan MehboobView Answer on Stackoverflow
Solution 9 - IosmkhoshpourView Answer on Stackoverflow
Solution 10 - IosTejasView Answer on Stackoverflow
Solution 11 - IosDhaval GevariyaView Answer on Stackoverflow
Solution 12 - IosUmmar AhmedView Answer on Stackoverflow
Solution 13 - IosCory BilleaudView Answer on Stackoverflow