Rohan Vasishth
Rohan Vasishth

Reputation: 241

UITableViewCell Not Displaying Data - Swift Playgrounds

I have been trying to implement some custom cells in a table view I created in a view controller in the swift playgrounds iPad app. Please note that I have seen a lot of the other stack overflow posts dealing with similar issues but have not been able to find a solution that fixes my problem.

Here is my 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)
    }

}

The following is my view controller code:

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

I don't know what the problem is. It seems that the cells simply do not display. Any help would be appreciated!

Upvotes: 2

Views: 293

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

Your problem is that you don't have a nib (or xib) file to do an awakeFromNib from.

Try doing this for your CustomCell instead:

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

}

Upvotes: 4

Related Questions