ios - 类型 'UIView' 的值没有成员 'tableView'

标签 ios swift uitableview

我有错误:

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/

相关文章:

ios - 找到多个匹配的代码签名身份(即证书和私钥对)

iphone - 如何在 iOS 中的其他 View 后面添加一个 View

ios - 一起使用 Google Analytics SDK 和 Firebase SDK

swift - 如何找到与x相差最小的字典值[Swift]

ios - 自定义 UITableview 中的单个单元格

ios - NSSortDescriptor 在查看详细 View 并返回后按字母顺序重新排序单元格

ios - 如何为带有部分的表格 View 单元格设置备用背景颜色

ios - 如何从特定索引路径加载 Collection View 单元格?

swift - 如何从默认日历中删除所有事件

iphone - viewController 中的 pushViewController 和 tableViewController