我正在尝试在 Swift 中设置自定义 UITableViewCell。我也创建了 XIB 文件和 swift 文件。但是,当我运行该应用程序时,单元格中并没有填充我的自定义单元格,而是一个完全空白的单元格。
我已将我的手机注册到:
self.myTableView.registerClass(customCell.self, forCellReuseIdentifier: "cell")
我还设置了以下内容:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCellWithIdentifier("cell") as! customCell
cell.descriptionLabel.text = "test"
return cell
}
此外,我在自定义单元格中设置了指向 UILabel 的链接。当我尝试更新它时,应用程序崩溃了。该行是 cell.descriptionLabel.text = "test"
,如上所示。
最佳答案
您必须注册 nib,而不是注册 UITableViewCell
类,例如:
self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
nibName
是 XIB 文件的名称,但没有 .xib
扩展名。
关于ios - 自定义 UITableViewCell 为空白(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38543016/