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
Change the background color of Table View Cell. In the Document Outliner, select the Table View's cell. Click on the Attributes inspector. In the View section set the background color to red: 108, green: 255, blue: 146, alpha 1.
Find the view or view controller you want to change the background color of. Open it up in the interface builder and open the Attributes Inspector. Find the background color field and set it to the color you want.
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
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.contentView.backgroundColor = UIColor.yellowColor()
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.contentView.backgroundColor = UIColor.yellow
}
If you want to achieve that via storyboard then select "Content View" of "table view cell" in your tableView then in attribute Inspector go to View and change its color like so:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With