cellForRowAt indexPath 函数未调用 - Swift tableView



我有一个表视图,它显示在我的故事板和预览中,但是当我在我的设备上运行它时,它没有显示。 调试时,我意识到问题似乎是它从未运行cellForRowAt indexPath函数。 为什么这段代码不执行该函数,我希望它输出 2 行。

编辑:正在调用 numberOfRowsInSection 并为 data.count 返回 2

这是我的视图控制器代码:

import UIKit
class WRViewController: PopupViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var wrNo: UILabel!
@IBOutlet weak var tableView: UITableView!
struct Contact {
var name:String
var type:String
var phone:String
}
var data:[Contact]=[Contact]()
override func viewDidLoad() {
super.viewDidLoad()
data.append(Contact(name:"John Doe",type:"Builder",phone:"1234567890"))
data.append(Contact(name:"Jane Doe",type:"Contractor",phone:"0987654321"))
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()
}
override func prepareToOpen() {
super.prepareToOpen()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath)
let contactView = data[indexPath.row]
cell.text = contactView.name
cell.image = contactView.type
cell.text = contactView.phone
return cell
}
}

问题是您没有提供足够的自动布局约束来确定此表视图的高度。因此它没有高度。因此,它不需要显示任何单元格。因此,它不要求任何细胞。

情节提要和/或视图调试器已经非常清楚地警告您这是一个问题,但您没有注意。嗯,注意!添加高度约束或顶部和底部约束,以便此表视图具有高度。

(请注意,问题可能不仅在于表视图,还在于它垂直固定到的任何内容。但是你没有提供这方面的信息,所以不可能从这里说。

最新更新