表视图未加载数据 [英] table view is not loading data
问题描述
我正在运行以下代码,并且在 UIViewController
中嵌套了一个 UITableView
.代码应该使用 nib
来填充行,但由于某种原因,当 UITableView
加载时没有任何显示.下面是与具有嵌入式 tableview
的 UIViewController
关联的以下类.有谁知道为什么这可能无法加载?
I am running the following code, and have a UITableView
nested within a UIViewController
. The code should be using a nib
to populate the rows, but for some reason nothing shows up when the UITableView
loads. Below is the following class associated with the UIViewController
that has an embedded tableview
. Does anyone know why this may not be loading?
我尝试过的代码:
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()
/代码>.
connect UITableView
dataSource and delegate
在 referencingOutlets
的 UIViewController
中.如下图,
connect UITableView
dataSource and delegate
in UIViewController
of the referencingOutlets
. Like as below,
这篇关于表视图未加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!