IOS Swift – How to change background color of Table View

iosswiftuitableview

I have a simple table view, I can change the colors of cells, but when trying to change the color of Table View (Background part) it is not working… I tried it via Storyboard… Can someone please help

Best Solution

First set the background color of the tableView in viewDidLoad like below:

override func viewDidLoad() {
    super.viewDidLoad()   
    self.tableView.backgroundColor = UIColor.lightGrayColor()
}

Now add this method:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.backgroundColor = UIColor.clearColor()
}

In Swift 3, use below methods instead of above one:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.backgroundColor = UIColor.lightGray
}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.backgroundColor = UIColor.clear
}