user6313237
user6313237

Reputation:

Value of type 'UIView' has no member 'tableView'

I have the error:

Value of type 'UIView' has no member 'tableView'

occur when I attempt to change the background of the tableView in the viewWillAppear method.

I expected the table view would already be initialised from the viewDidLoad method.

Where should I be trying to change the colours for the tableview? Should I also be changing the other background colours in the same place?

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
  }
}

Upvotes: 2

Views: 5704

Answers (3)

Bhavin Bhadani
Bhavin Bhadani

Reputation: 22374

You can make it an instance variable of UITableView like

 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()
     }
}  

Upvotes: 2

Iyyappan Ravi
Iyyappan Ravi

Reputation: 3245

Declare the tableview in global value, then use the tableview in class, see the below code,

     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() 
      }
}

hope its helpful

Upvotes: 1

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52163

This is not the right way of accessing a subview within a view. You can use viewWithTag: to get the subview by tag:

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

...

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

Alternatively, you make tableView an instance variable if you need to access it in more than just 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()
    }
}

Upvotes: 1

Related Questions