swift - TableView 未加载数据

标签 swift uitableview

我正在运行以下代码,并在 UIViewController 中嵌套了一个 UITableView。代码应该使用 nib 来填充行,但由于某种原因,当 UITableView 加载时没有任何显示。下面是与具有嵌入式 tableviewUIViewController 关联的以下类。有谁知道为什么这可能无法加载?

我试过的代码:

import UIKit
import Firebase

class Today: UIViewController, UITableViewDelegate, UITableViewDataSource  {

    var anArray: [String] = ["a", "b", "c"]

    @IBOutlet weak var myTableView: UITableView!
    override func viewDidLoad() {

        let nib = UINib.init(nibName: "ScheduleCellTableViewCell", bundle: nil)

        self.myTableView.registerNib(nib, forCellReuseIdentifier: "cell")

        self.myTableView.reloadData() //<<<<<<<<<<<<< RELOADS TABLEVIEW

    }//viewDidLoad() ends here


    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //return the count of the number of groups
        return anArray.count
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ScheduleCellTableViewCell

        let val = indexPath.row

        cell.testLabel.text = anArray[val]

        return cell
    }

    public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 160.0
    }
}

最佳答案

您不需要为 viewDidLoad() 上的 UITableView reloadData()

referencingOutletsUIViewController 中连接 UITableView dataSource 和 delegate。如下所示,

enter image description here

关于swift - TableView 未加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44938900/

相关文章:

ios - Swift UITextField 文本通过 TableViewCell 中的按钮操作清除

crash - UITableView 崩溃而没有错误

ios - 重新加载 cellForRowAtIndexPath 时出现问题 - Objective C

ios - 将服务器数据保存到核心数据,在后台线程问题

ios - 为什么在 Storyboard中看不到我的自定义 View 的预览?

swift - UITextView 点击事件设置 UITableView 可见

ios - SelfSizing Cell 仅适用于一个原型(prototype)电池

swift - 在 Swift 中声明随机数量的对象

ios - 关键观察者没有停止更新新值?

ios - 在主视图 Swift 中嵌入 subview