22

I'm using UITableView in a swift application, and using my own custom UITableViewCell.

When trying to hide the empty cells in the UITableView, its still have the white background.

Under the UITableView I have a background image (UImageView).

I'v already try to hide those cells, and actually they are hidden, but I still have a white background, using this code:

override func viewDidLoad() {
    super.viewDidLoad()
    var tblView =  UIView(frame: CGRect(x: 0,y: 0,width: 0,height: 0))
    tblView.backgroundColor = UIColor.clearColor()
    tableView.tableFooterView = tblView

    var nipName=UINib(nibName: "CustomCell", bundle:nil)
    self.tableView.registerNib(nipName, forCellReuseIdentifier: "CustomCell")
}
user3210784
  • 499
  • 1
  • 4
  • 11

6 Answers6

31

A cleaner solution is:

tableView.tableFooterView = UIView(frame: CGRectZero)

Nothing else is needed.

Jorge
  • 1,066
  • 8
  • 16
  • 1
    @user3210784 I believe perhaps this must be the accepted answer as it does the job with much less code than the accepted answer ? – Ashkan Kh. Nazary Sep 30 '15 at 11:53
  • absolutely correct answer. A little addition: you need to mention the IBOutlet for tableView to make this work. – pulp Dec 21 '15 at 21:09
25

Here Is the solution:

tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
tableView.tableFooterView?.isHidden = true
tableView.backgroundColor = UIColor.clear
lesyk
  • 3,979
  • 3
  • 25
  • 39
user3210784
  • 499
  • 1
  • 4
  • 11
3

SWIFT 3

tableView.tableFooterView = UIView(frame: .zero)

Jason Silver
  • 706
  • 7
  • 10
0

Another simpler solution for this issue is setting the background with an image for your tableView.The image should be plain white if you just need a white background for your non-data cells or you can set your custom image as well for your tableview background.So data with cells would have normal background and for the non-data cell it will show your background image:

 self.tableViewName.backgroundColor = UIColor (patternImage: UIImage(named:
 "imageName.jpg")!)
Anmol Kukreja
  • 110
  • 1
  • 8
0

Either of these work for me in Swift 3.0

Method - 1

let footerView =  UIView(frame: CGRect.zero)
tableView.tableFooterView = footerView
tableView.tableFooterView?.isHidden = true
tableView.backgroundColor = UIColor.clear()

Method - 2

 tableView.tableFooterView = UIView(frame: CGRect.zero)
byJeevan
  • 3,728
  • 3
  • 37
  • 60
ICL1901
  • 7,632
  • 14
  • 90
  • 138
0

Swift 3 : Simplest way

tableView.tableFooterView = UIView()
Musa almatri
  • 5,596
  • 2
  • 34
  • 33