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

13

134

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?

Tumid answered 24/2, 2015 at 23:45 Comment(0)
P
450

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

tableView.tableFooterView = UIView()
Premillenarian answered 24/2, 2015 at 23:48 Comment(5)
Short and effective! Worked like charm!Sophocles
What does this mean? Could you Elaborate?Vries
This answer is still valid in Swift 3. Also, if you set the tableview background color the same as the main views background color, it'll make it look as if the footer is gone.Daph
Is there a similar solution for NSTableView? It does not have a tableFooterView.Bodnar
SWIFT 3.0 works great with tableView.tableFooterView = UIView()An
B
23

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

enter image description here

Burress answered 24/4, 2017 at 20:57 Comment(0)
A
7

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
Albrecht answered 28/9, 2016 at 10:8 Comment(3)
Sorry but doesn't it look like a hack?Littlefield
Nope, this is a completely valid way of doing it. If you wanted to set a custom tableFooterView, you'd create a custom UIView, before setting it to tableFooterView. Then resetting it to nil is just returning it to its default value.Albrecht
I see. Thanks) It just looks a bit weird for me as I am new to swiftLittlefield
S
7

Setting the tableFooterView should remove extra cell like views.

   @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.tableFooterView = UIView(frame: .zero)
        }
    }
Showery answered 12/11, 2018 at 4:40 Comment(0)
I
4
tableView.tableFooterView = UIView.init(frame: CGRect.zero)

Call this code in viewDidLoad method.

Iodate answered 2/2, 2017 at 7:37 Comment(0)
B
3

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

Briard answered 2/2, 2017 at 6:15 Comment(0)
K
3

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()
}
Kraal answered 7/2, 2018 at 3:4 Comment(0)
S
3

Based on above answers in swift.

tableView.tableFooterView = UIView(frame: .zero)
Secret answered 6/3, 2018 at 20:31 Comment(0)
L
3

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

}
Lianaliane answered 21/8, 2018 at 7:11 Comment(0)
A
1

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.

Anamariaanamnesis answered 12/4, 2017 at 6:46 Comment(0)
P
1

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

tblOutletName.tableFooterView = UIView()

Principate answered 27/10, 2019 at 18:0 Comment(0)
F
-2

Abobe anwer is right. We can achieve this by adding

tableView.tableFooterView = UIView() // to remove extra cells in tableView
Feininger answered 12/6, 2018 at 5:33 Comment(0)
P
-3

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.

Parenthesize answered 23/6, 2017 at 19:39 Comment(1)
This will not display the last item in the array.Kraal

© 2022 - 2024 — McMap. All rights reserved.