UITableViewCell不显示数据 - Swift Playgrounds

时间:2017-03-29 02:39:12

标签: ios swift uitableview swift-playground custom-cell

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

这是我的CustomCell类:

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
    }
}

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

1 个答案:

答案 0 :(得分:4)

您的问题是,您没有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")
    }

}