John Doe
John Doe

Reputation: 821

Why UITableView does not call my customCell?

I've created UITableView and UITableViewCell programmatically. In my ViewController - viewDidLoad I do:

self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.registerClass(newsCell.self, forCellReuseIdentifier: "newsCell")

later use it as:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("newsCell", forIndexPath: indexPath) as! newsCell

    return cell
}

My newsCell class(shortly):

class newsCell: UITableViewCell {
    let scoreLabel = UILabel()

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    print("init")

    self.addSubview(self.scoreLabel)
}
}

but I do not even get init on logs, so it does not call my custom cell at all. What is a problem?

Upvotes: 0

Views: 663

Answers (1)

michael wang
michael wang

Reputation: 561

Try this code below:

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

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

and forced unwrapping is dangerous. You should do it like this:

if let cell = self.tableView.dequeueReusableCellWithIdentifier("newsCell", forIndexPath: indexPath) as? newsCell{}

Upvotes: 2

Related Questions