UITableViewCell 不显示数据 - Swift 游乐场

UITableViewCell Not Displaying Data - Swift Playgrounds

我一直在尝试在 table 视图中实现一些自定义单元格,我在 swift 游乐场 iPad 应用程序的视图控制器中创建了该视图。请注意,我已经看到很多其他处理类似问题的堆栈溢出帖子,但未能找到解决我的问题的解决方案。

这是我的 CustomCell Class:

class CustomCell: UITableViewCell {

    let projectImage = UIImageView()
    //let projectDivider = UIView()
    let projectTitle = UILabel()

    override func awakeFromNib() {
        projectImage.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        //projectDivider.frame = CGRect(x: 100, y: 0, width: 20, height: 100)
        projectTitle.frame = CGRect(x: 120, y: 0, width: 200, height: 100)

        self.contentView.addSubview(projectImage)
        //self.contentView.addSubview(projectDivider)
        self.contentView.addSubview(projectTitle)
    }

}

以下是我的视图控制器代码:

class AboutViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let tableView = UITableView()
    let cellReuseIdentifier: String = "cell"

    var projectTitles = ["Madhubani Coffee", "World Savvy", "Zipshare", "CrashThatCode", "Future Plans"]
    var images: [UIImage] = [UIImage(named: "IMG_0061.JPG")!, UIImage(named: "IMG_0064.JPG")!, UIImage(named: "IMG_0062.JPG")!, UIImage(named: "IMG_0062.JPG")!, UIImage(named: "IMG_0062.JPG")!]

    override func viewDidLoad() {
        view.backgroundColor = UIColor.white

        tableView.frame = CGRect(x: -82.5, y: 250, width: 600, height: 600)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.white

        self.tableView.register(CustomCell.self as AnyClass, forCellReuseIdentifier: cellReuseIdentifier)
        view.addSubview(tableView)

     }

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath) as! CustomCell

        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: cellReuseIdentifier) as! CustomCell
        }

        cell.projectImage.image = images[indexPath.row]
        cell.projectTitle.text = projectTitles[indexPath.row]

        cell.clipsToBounds = true

        return cell
    }
}

我不知道是什么问题。似乎细胞根本不显示。如有任何帮助,我们将不胜感激!

您的问题是您没有 nib(或 xib)文件来执行 awakeFromNib

尝试为您的 CustomCell 执行此操作:

class CustomCell: UITableViewCell {

    let projectImage = UIImageView()
    //let projectDivider = UIView()
    var projectTitle = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        projectImage.frame = CGRect(x: 0, y: 0, width: 100, height: 30)
        //projectDivider.frame = CGRect(x: 100, y: 0, width: 20, height: 100)
        projectTitle.frame = CGRect(x: 10, y: 0, width: 200, height: 30)
        projectTitle.font = UIFont.systemFont(ofSize: 20.0)
        //self.contentView.addSubview(projectImage)
        //self.contentView.addSubview(projectDivider)
        self.contentView.addSubview(projectTitle)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}