类型的价值' UIView'没有会员' tableView'

时间:2016-05-10 04:23:33

标签: ios swift uitableview

我有错误:

  

类型的价值' 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
  }
}

3 个答案:

答案 0 :(得分:2)

您可以将其设为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()
     }
}  

答案 1 :(得分:1)

这不是访问视图中的子视图的正确方法。您可以使用viewWithTag:按标记获取子视图:

override func viewDidLoad() {
  ...
  tableView.tag = 100
  view.addSubview(tableView)
}

...

override func viewWillAppear(animated: Bool) {
  ...
  if let tableView = view.viewWithTag(100) {
    tableView.backgroundColor = UIColor.blackColor()
  }
}

或者,如果您需要在tableView以外的地方访问实例变量,则可以viewWillAppear

class SettingsViewController: UIViewController ... {
    var tableView: UITableView!
    ...

    override func viewDidLoad() {
      ...
      self.tableView = UITableView(frame: view.bounds, style: .Plain)
      ...
    }

    override func viewWillAppear(animated: Bool) {
      ...
      self.tableView.backgroundColor = UIColor.blackColor()
    }
}

答案 2 :(得分:1)

以全局值声明tableview,然后在类中使用tableview,参见下面的代码,

     class ViewController: UIViewController {
            var tableView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        tableview.frame = self.view.bounds
        tableView.delegate = self
        tableView.dataSource = self
        tableView.registerClass(settingsCell.self, forCellReuseIdentifier: NSStringFromClass(settingsCell))
        self.view.addSubview(tableView)
      }

    override func viewWillAppear(animated: Bool) {
            if nightMode == true {
                view.backgroundColor = UIColor.blackColor()
                tableView.backgroundColor = UIColor.blackColor() 
      }
}

希望它有用

相关问题