【发布时间】:2018-11-17 16:27:48
【问题描述】:
我正在使用我的第一个 UITableView 并且只收到一些回调。在下面的代码中,我想用测试数据填充 TableView。要求节数和行数的 2 个回调工作正常。永远不会调用允许填充单元格的回调。我的故事板上有一个 TableView 对象,一个带有标识符“cellReuseIdentifier”的原型单元和实例变量 btListView 的出口。任何指导都将不胜感激。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
var tableData: [String] = []
@IBOutlet weak var btListView: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
for i in 0...5
{
tableData.append("(\(i)")
}
btListView.dataSource = self
}
}
func numberOfSections(in tableView: UITableView) -> Int
{
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = btListView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier")! as UITableViewCell
let temp = tableData[indexPath.row]
cell.textLabel?.text = temp
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return tableData.count
}
【问题讨论】:
-
你确定在它调用
numberOfRowsInSection方法时你已经拥有'tableData'。尝试在viewDidLoad方法的末尾添加btListView .reloadData()btListView.dataSource = self这一行 -
是的,有数据。为了确定,我添加了打印语句。 reloadData() 没有帮助。我创建了一个测试应用程序,使用相同的代码并删除了一堆其他东西(文本和蓝牙代表),并且该应用程序工作正常。我删除并重新添加了表格视图和原始单元格,但没有继续。
-
所以我只是看了一下这两个应用程序之间的差异,而一个不起作用的应用程序在 StackView 中使用了 TableView。在我将它移动到视图的顶层(堆栈视图之上)后,将触发回调。我不确定我错过了什么。
-
你是如何设置 StackView 和 TableView 的约束的?如果 tableView 最终的高度为零,则不会显示任何行,并且永远不会调用
cellForRowAt。 -
我没有更改高度或宽度,但既然您提到了它,当列表视图包含在堆栈视图中时,列表视图根本不会显示。
标签: ios swift uitableview callback datasource