0
votes

When implementing a UITableView, I added a background image and set the table view to transparent. However, the sides of the table remain clear even when I change the table view cells to a different color (in order to see the text better), as shown in this image: Table View Cell.

The code I added to get the inner part of the table view:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.backgroundColor = UIColor(white: 1, alpha: 0.25)
        cell.accessoryView?.backgroundColor = UIColor(white: 1, alpha: 0.25)
        cell.contentView.backgroundColor = UIColor(white: 1, alpha: 0.25)
        cell.tintColor = UIColor(white: 1, alpha: 0.25)
}

And in viewWillAppear:

self.tableView.backgroundColor = UIColor(white: 1, alpha: 0.25)

I'm not sure why the sides don't show up as the same color. I thought it had something to do with the accessory button, but when attempting to change that as well, nothing was changed.

2

2 Answers

0
votes

The contentView doesn't cover the whole cell. Try making the setting its background color to the clear color. Then only the cell's background view will show through.

0
votes

Much of your code is sheer waste; it has nothing to do with the matter. The key lines are:

cell.backgroundColor = UIColor(white: 1, alpha: 0.25)
cell.contentView.backgroundColor = UIColor(white: 1, alpha: 0.25)

That's a silly thing to do, as you are overlaying the cell's translucent background color with another background color (the background color of the content view, which sits in front of the cell).

When you have a problem like this, solve it yourself by making a simpler, clearer test case. For example:

    cell.backgroundColor = .red
    cell.contentView.backgroundColor = .green
    cell.accessoryType = .checkmark

enter image description here

That makes it perfectly obvious what's going on: the green content view partially obscures the red cell. Your code does exactly the same sort of thing.