我在 Xcode 中创建了自定义单元格 View :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FeedCell
let iniStr = self.tableArray[indexPath.row]
let fullNameArr = iniStr.components(separatedBy: "||1||")
let daFirst = fullNameArr[0]
let daSecond = fullNameArr[1]
let finalco = daSecond.components(separatedBy: "||2||")
let fString = finalco[0]
cell.questionLabel?.text = daFirst
cell.answerLabel?.text = daSecond
return cell
}
class FeedCell: UITableViewCell {
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var answerLabel: UILabel!
}
然后连接 Storyboard中的所有内容,设置约束并注册类:self.tableView.register(FeedCell.self, forCellReuseIdentifier: "Cell")
当我使用 Subtitle 作为我的自定义表格单元格的类时,一切正常,但是当我在我的表格单元格中使用自定义 FeedCell 时,标签没有显示,但我可以选择单元格。
最佳答案
试试这段代码来调试连接
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FeedCell
let iniStr = self.tableArray[indexPath.row]
print(iniStr)
let fullNameArr = iniStr.components(separatedBy: "||1||")
let daFirst = fullNameArr[0]
let daSecond = fullNameArr[1]
print(daFirst)
print(daSecond)
let finalco = daSecond.components(separatedBy: "||2||")
let fString = finalco[0]
cell.questionLabel?.text = daFirst
cell.answerLabel?.text = daSecond
cell.questionLabel.backgroundColor = .red
cell.answerLabel.backgroundColor = .green
return cell
}
关于ios - 自定义 UITableViewCell 不显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47095560/