我正在运行以下代码,并在 UIViewController
中嵌套了一个 UITableView
。代码应该使用 nib
来填充行,但由于某种原因,当 UITableView
加载时没有任何显示。下面是与具有嵌入式 tableview
的 UIViewController
关联的以下类。有谁知道为什么这可能无法加载?
我试过的代码:
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()
。
在 referencingOutlets
的 UIViewController
中连接 UITableView
dataSource 和 delegate
。如下所示,
关于swift - TableView 未加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44938900/