【发布时间】:2016-05-10 04:23:33
【问题描述】:
我有错误:
“UIView”类型的值没有成员“tableView”
当我尝试在 viewWillAppear 方法中更改 tableView 的背景时发生。
我希望表格视图已经从 viewDidLoad 方法初始化。
我应该在哪里尝试更改 tableview 的颜色?我还应该在同一个地方更改其他背景颜色吗?
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
}
}
【问题讨论】:
-
不使用
UITableViewController或至少使用实例变量的目的是什么? -
您的 tableView 已在本地范围内。将它放在 viewDidLoad 之外的顶部。就目前而言,只有 viewDidLoad “看到”了它。请参阅 Bhavin 对 deets 的回答
-
我这样做主要是为了练习,我认为 UIViewController 需要我手动做更多的事情。 stackoverflow.com/questions/14465447/…
-
感谢 Adrian 和 Bhavin,我看到 tableView 只存在于 viewDidLoad 函数中。出于学术兴趣,错误是因为一旦 viewDidLoad 完成,tableView 将从编译器的内存中删除吗?还是因为 XCode 看不到 viewDidLoad 会在 viewWillAppear 之前运行,而它实际上应该可以工作?
标签: ios swift uitableview