Reputation: 12549
I'm trying to create a simple view with a UITableView in it. I want to change the table view's background color to purple via the backgroundColor property of the table view. Apple documentation says that you only need to set to nil the backgroundView property for it to work. In my case it does not work.
I have to call UITableView.appearance().backgroundColor = UIColor.purpleColor() for it to work instead of just UITableView.backgroundColor = UIColor.purleColor()
My question is why?
This is the code:
class TaskTableView: UIView {
var tableView = UITableView(frame: CGRectZero, style: .Plain)
override init(frame: CGRect) {
super.init(frame: frame)
tableView.backgroundView = nil
tableView.backgroundColor = UIColor.purpleColor()
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
tableView.translatesAutoresizingMaskIntoConstraints = false
addSubview(tableView)
let views = ["table": tableView]
addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("|[table]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
)
addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("V:|[table]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
)
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
}
Upvotes: 1
Views: 324
Reputation: 1751
The sample code provided above works fine for me, when creating a new view (the custom subclass TaskTableView
) programmatically:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let tableViewFrame : CGRect = CGRectMake(20, 20, 100, 300)
let taskTableView : TaskTableView = TaskTableView.init(frame: tableViewFrame)
view.addSubview(taskTableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
When running this code in a blank new project, using the class definition for TaskTableView as provided in the question, the result is a table view with the desired background color of purple.
That having been said, I can guess why you are having issues setting the background color of the table view. The issue may have to do with the view life cycle. The appearance protocol is applied to views just before they appear on screen. It is possible that you are instantiating a custom view instance too early in the view lifecycle, i.e. before the view's containing view controller has awaken from its nib. Therefore you are trying to set properties on a view which has not yet been deserialized, and therefore will not show the desired background color settings. See the following quote from Apple's Developer documentation.
If you plan to load instances of your custom view class from a nib file, you should be aware that in iOS, the nib-loading code does not use the initWithFrame: method to instantiate new view objects. Instead, it uses the initWithCoder: method that is part of the NSCoding protocol.
(reference: View Programming Guide for iOS)
It is worth pointing out that the UITableView class is typically only added as a sub view to a UIViewController, with the same view controller also acting as the Table View's delegate and data source. Apple recommends that you use their pre-configured UITableViewController, which is a UIViewController with a UITableView as its view, and also acts as the data source and delegate. Adding the UITableView directly as a sub-view of a custom view class (as you have done) is not a best practice and is therefore discouraged.
For more information on working with UITableView class, see the official Apple documentation:
Upvotes: 1