iOS Swift - How to change background color of Table View?

前端 未结 6 1494
情书的邮戳
情书的邮戳 2021-01-31 02:46

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.

相关标签:
6条回答
  • 2021-01-31 03:24

    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:

    0 讨论(0)
  • 2021-01-31 03:28

    You have:

    1. Cell background AND
    2. Cell ContentView

    Put ContentView like Default (transparent) in SB, and in your Cell's Method in awakeFromNib, just:

    self.backgroundColor = UIColor.SomeColor
    
    0 讨论(0)
  • 2021-01-31 03:29
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        cell.contentView.backgroundColor = UIColor.yellowColor()
    }
    

    Swift 3

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.contentView.backgroundColor = UIColor.yellow
    }
    
    0 讨论(0)
  • 2021-01-31 03:30

    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
    }
    
    0 讨论(0)
  • 2021-01-31 03:30

    I'm a little late to the game! But, none of the above answers worked for me, so just going to share, what I found worked, in case anyone else is jumping on this thread.

    NOTE: I also have my own custom cell class. Not sure if that will affect your code!

    @IBDesignable class YourViewController: UITableViewController {
    
    //IBInspectable and IBDesignable makes the properties accessible in storyboard
    @IBInspectable var tableViewBackground: UIColor?
    @IBInspectable var cellViewBackground: UIColor?
    
    //in case you want to customize the text color, as well!
    @IBInspectable var customTextColor: UIColor?
    
    
    override func viewDidLoad() {
    //your code
    self.tableView.backgroundColor = tableViewBackground
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath:    IndexPath) -> UITableViewCell {
    
        let cell = UITableViewCell()
        cell.textLabel?.textColor = customTextColor
        cell.backgroundColor = cellViewBackground
        return cell
    }
    //the rest of your code
    }
    

    Finding your inspectables:

    1). In the Storyboard menu for the viewController of your tableView, click on yourViewController. The first dropdown inside the viewController. (The first dropdown will contain your tableView and cellView. It can also contain other goodies depending on your particular project).

    2). In the attributes inspector, you'll see a heading with the name of your viewController and the @IBInspectable properties we defined above!!

    3). You can then change them however you want there.

    Hope this helps!

    0 讨论(0)
  • 2021-01-31 03:46

    use this code for change tableView color

    override func viewDidLoad() {
            super.viewDidLoad()
    
           // define tableview background color
            self.tableView.backgroundColor = UIColor.clearColor()
    }
    

    for change tableView cell color

    cell.backgroundColor = UIColor.clearColor()
    
    0 讨论(0)
提交回复
热议问题