0

I am using swift 3.0 and have a TableView in it. Everything is working great except that I only have 2 rows returning for now, but the TableView is showing additional blank rows. I was wondering if there was any way to eliminate those rows?

Below there is how my simple app looks right now: as you can see, I have 2 rows and I want to eliminate the other rows, because they do not need to be there. I am new to TableViews and I thought that this piece of code controls how many rows should appear:

 func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
   return 2
 }

Any suggestions would be great

My TableView

Vasilisa
  • 4,604
  • 3
  • 20
  • 25
user1591668
  • 2,591
  • 5
  • 41
  • 84

4 Answers4

3
tableView.tableFooterView = UIView()
chengsam
  • 7,315
  • 6
  • 30
  • 38
2

UIKit does not create the empty rows when the tableView has a footerView displayed below the table cells. you can add one with zero height so its not visible to the user. In the viewDidLoad method of your tableView .create a new UIView with a zero rect frame and use it to set the tableFooterView property of the tableView:

yourTableViewName.tableFooterView = UIView(frame: CGRect.zero)
Joe
  • 8,868
  • 8
  • 37
  • 59
1

Give footer to the UITableView.
From that your all blank or extra rows are removed.

on viewDidLoad() put below code

Swift 3.0

yourTableViewName.tableFooterView = UIView()
John Paul
  • 12,196
  • 6
  • 55
  • 75
john afordis
  • 317
  • 3
  • 15
1

Just add following code on viewDidLoad():

self.tblViewObj.tableFooterView = UIView()
Sarabjit Singh
  • 1,814
  • 1
  • 17
  • 28