【问题标题】:Value of type 'UIView' has no member 'tableView'“UIView”类型的值没有成员“tableView”
【发布时间】: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


【解决方案1】:

您可以将其设为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) 这没有定义tableView“全局”(具有不同的含义)。这使它成为一个实例变量。 2) 声明实例变量时无需创建临时的UITableView 实例。
  • 我对 Swift 还很不熟练,但 tableView 实例变量的声明不应该只是 var tableView:UITableView 吗?为什么要添加= UITableView() 部分?
  • = UITableView() 创建一个新的表视图。但随后您在viewDidLoad 中创建另一个。创建初始表视图没有意义。
  • @rmaddy 没有完全声明 tableView 我在尝试访问 tableView.background 时打开 nil 值时出错。因此,我声明了变量table view,去掉了viewDidLoad中的第二个声明,并将viewDidLoad中的frame修改为view.bounds。 Bhavin 建议您的原始解决方案对 viewDidLoad 的这些更改是正确的
【解决方案2】:

这不是在视图中访问子视图的正确方法。可以使用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()
  }
}

或者,如果您需要在不止viewWillAppear 中访问它,您可以将tableView 设为实例变量:

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

【讨论】:

  • 或将tableView 设为实例变量。这会好很多,因为代码可能需要访问tableView 而不仅仅是viewWillAppear
【解决方案3】:

在全局值中声明tableview,然后在class中使用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() 
      }
}

希望对你有帮助

【讨论】:

  • 为什么包含 IBOutlets?
猜你喜欢
  • 2023-03-09
  • 2016-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多