ajayb
ajayb

Reputation: 633

UITableView Background Color Swift

I'm looking to change the color for the background of my UITableView using Swift. I want to make both the cell background as well as the actual table's background a specific color. So far I have created a simple table with this code:

import UIKit

class MyTripsViewController: UIViewController, UITableViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
        cell.textLabel?.text = "Test"
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

Upvotes: 1

Views: 6093

Answers (1)

shpasta
shpasta

Reputation: 1933

Cell property backgroundColor changes background color. To setup color:

cell.backgroundColor = UIColor(red: 0.1, green: 0.2, blue: 0.3, alpha: 1.5)

Or use predefined colors:

cell.backgroundColor = UIColor.blueColor()

Upvotes: 3

Related Questions