【问题标题】:Unable to load UITableView in Playground无法在 Playground 中加载 UITableView
【发布时间】:2018-05-14 21:48:46
【问题描述】:

我正在尝试在 Swift 操场上添加 UITableView,但由于某种原因它没有加载。这是我的代码:

class MyViewController : UIViewController,UITableViewDelegate,UITableViewDataSource {
    let content = ["One", "Two"]
    var tableView = UITableView()

    override func loadView() {
        let view = UIView()
        view.backgroundColor = .red
        self.view = view
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .blue
        self.tableView = UITableView(frame: self.view.bounds, style: .plain)
        self.tableView.delegate      =   self
        self.tableView.dataSource    =   self
        self.view.addSubview(self.tableView)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.content.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        if cell.detailTextLabel == nil {
            cell = UITableViewCell(style: .value1, reuseIdentifier: "cell")
        }
        cell.textLabel?.text = self.content[indexPath.row]
        return cell
    }
}

PlaygroundPage.current.liveView = MyViewController()

你们有谁知道为什么 tableview 没有加载到视图中?

【问题讨论】:

  • 可能是因为表格视图是空的?
  • 确认viewDidLoadself.view.bounds的值。并且不要忘记您需要实现各种表格视图数据源和委托方法。
  • 顺便说一句 - 虽然可能与您的问题无关,但您的 loadView 是不必要的。只需在viewDidLoad 中设置背景颜色即可。
  • 我更新了我的代码
  • 您是否将PlaygroundPage.current.liveView 设置为您的控制器实例?

标签: ios swift uitableview swift-playground


【解决方案1】:

您忘记将单元格注册到表格视图。 添加表格视图后执行此操作。

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

【讨论】:

    【解决方案2】:

    没有显示单元格的原因是

    • 你没有注册单元格(正如@kirander 已经提到的)
    • 您的表格视图根本没有大小。

    你不应该在 viewDidLoad 中做布局/几何的东西,因为框架和边界在这个方法的调用时是不可预测的。

    您可以使用自动布局将表格视图连接到外部视图的尺寸,或者更新 viewDidAppear 中的框架:

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .blue
        print (self.view.bounds)    // (0.0, 0.0, 0.0, 0.0)
        self.tableView = UITableView(frame: self.view.bounds, style: .plain)
        self.tableView.delegate      =   self
        self.tableView.dataSource    =   self
        self.view.addSubview(self.tableView)
    
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        super.viewDidLoad()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        print (self.view.bounds)    // (0.0, 0.0, 375.0, 668.0)
        self.tableView.frame = self.view.bounds
    }
    

    或者,当使用自动布局时:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.view.backgroundColor = .blue
        print (self.view.bounds)    // (0.0, 0.0, 0.0, 0.0)
        self.tableView = UITableView(frame: self.view.bounds, style: .plain)
        self.tableView.delegate      =   self
        self.tableView.dataSource    =   self
        self.view.addSubview(self.tableView)
    
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
    
    
        super.viewDidLoad()
    }
    

    【讨论】:

    • 你不觉得在viewWillLayoutSubviews上改变子视图的框架更合适吗?
    • @Adri 是的,你是完全正确的。我几乎总是忘记这一点......但我建议viewDidLayoutSubviews
    【解决方案3】:

    您应该在底部的override func loadView() 中添加super.loadView() 并适当地访问该方法的超类版本 有关superLoadView 功能的更多信息, 按照这些链接super ClassloadView()

    【讨论】:

      【解决方案4】:

      当您在viewDidLoad 上创建表格视图时,尝试打印self.view.bounds 的大小,我怀疑它会是.zero

      我试过了,效果很好:

      import UIKit
      import PlaygroundSupport
      
      class MyViewController : UIViewController, UITableViewDataSource {
      
          let content = ["One", "Two"]
      
          var tableView: UITableView!
      
          override func viewDidLoad() {
              super.viewDidLoad()
      
              tableView = UITableView(frame: .zero)
              tableView.translatesAutoresizingMaskIntoConstraints = false
              tableView.dataSource = self
              view.addSubview(tableView)
      
              NSLayoutConstraint.activate([
                  tableView.topAnchor.constraint(equalTo: view.topAnchor),
                  tableView.rightAnchor.constraint(equalTo: view.rightAnchor),
                  tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
                  tableView.leftAnchor.constraint(equalTo: view.leftAnchor)
                  ])
          }
      
          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return content.count
          }
      
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
              let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
              cell.textLabel?.text = content[indexPath.row]
              return cell
      
          }
      
      }
      
      PlaygroundPage.current.liveView = MyViewController()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多