How to set background color of cells in static table swift Xcode 6

Boštjan Lutar picture Boštjan Lutar · Apr 10, 2015 · Viewed 8.2k times · Source

I have static table and I set background color for tableview -> tableView.backgroundColor = UIColor.redColor() and when I have etc. 3 rows in static table then is tableview colored and 3 rows are white how can I fix background color for this cells? This code are not working for static table ->

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell


    cell.textLabel!.text = numbers[indexPath.row]
    cell.backgroundColor = UIColor.greenColor()

    return cell
}

Answer

williammr picture williammr · Sep 20, 2015

In static Table View you don't have to implement cellForRowAtIndexPath. So I suggest you to implement the following code:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
   // Change the color of all cells     
   cell.backgroundColor = UIColor.greenColor()
}