ios - 自定义 UITableViewCell 不显示标签

标签 ios objective-c swift uitableview

我在 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/

相关文章:

objective-c - 我的 admob 横幅显示在顶部

ios - SendBird iOS SDK 错误 14000, "Fail to start messaging"的可能原因是什么?

iphone - 涉及NSString的内存泄漏

ios - 如何在 2D 游戏 map 上找到最安全/最佳点?

ios - 为什么我会收到错误 -[__NSArrayM length] : unrecognized selector sent to instance 0x7fd4d15211d0 while assigning data to custom tableView

swift - 为什么我的数据没有显示在表格 View 中?

ios - DispatchQueue 不等待异步函数完成

java - 从 Cloud Firestore 获取 map 数据

iphone - cell.titleLabel的字体设置应该在cellForRowAtIndexPath:方法中的什么位置?

objective-c - ReactiveCocoa 信号,用于在观察身份验证状态时获取数据