我有错误:
Value of type 'UIView' has no member 'tableView'
当我尝试在 viewWillAppear
方法中更改 tableView 的背景时发生。
我预计 TableView 已经从 viewDidLoad
方法初始化。
我应该在哪里尝试更改桌面 View 的颜色?我是否还应该更改同一位置的其他背景颜色?
class settingsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
// Table view setup
let tableView = UITableView(frame: view.bounds, style: .Plain)
tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(settingsCell.self, forCellReuseIdentifier: NSStringFromClass(settingsCell))
view.addSubview(tableView)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
if nightMode == true {
view.backgroundColor = UIColor.blackColor()
view.tableView.backgroundColor = UIColor.blackColor() // Error here..
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return settings.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(NSStringFromClass(settingsCell), forIndexPath: indexPath) as! settingsCell
// Fetch setting
let cellSetting = settings[indexPath.row]
// Configure Cell
cell.textLabel?.text = cellSetting.name
return cell
}
}
最佳答案
您可以将其设为 UITableView
的实例变量,例如
class settingsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView:UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Table view setup
tableView = UITableView(frame: view.bounds, style: .Plain)
tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(settingsCell.self, forCellReuseIdentifier: NSStringFromClass(settingsCell))
view.addSubview(tableView)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
if nightMode == true {
view.backgroundColor = UIColor.blackColor()
tableView.backgroundColor = UIColor.blackColor()
}
}
关于ios - 类型 'UIView' 的值没有成员 'tableView',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37129238/