0

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?

John Doe
  • 811
  • 4
  • 14
  • 26
  • I think you should move your initialization-code to the *init*-method instead of awakeFromNib(). – TMob Apr 15 '16 at 07:29
  • Yes. I think so. `awakeFromNib` will be fire when you create from Nib File. – Long Pham Apr 15 '16 at 07:30
  • 1
    Have a look at this: http://stackoverflow.com/a/15239265/3981769 – TMob Apr 15 '16 at 07:32
  • did u create the nib for tableviewcell ? – Reshmi Majumder Apr 15 '16 at 07:32
  • @longpham I'm trying this `init() { super.init(frame: CGRectZero) self.initialize() }` but it gives me an error `Must call a designated initializer of the superclass 'UITableViewCell'` – John Doe Apr 15 '16 at 07:37
  • If you have connected datasource and delegate to tableview on scoreboard then u don' t need to add self.tableView.registerClass(newsCell.self, forCellReuseIdentifier: "newsCell") – Reshmi Majumder Apr 15 '16 at 07:37
  • @ReshmiMajumder I've created all programmatically – John Doe Apr 15 '16 at 07:38
  • If you create the cell programmatically, `awakeFromNib` is never called, because there is no nib file. You need to use the designated initializer. – vadian Apr 15 '16 at 07:41
  • then awakeFromNib will never be called you need to use override func init(style: UITableViewCellStyle, reuseIdentifier: String?) then addSubview – Reshmi Majumder Apr 15 '16 at 07:42
  • @vadian like this `override init(style: UITableViewCellStyle, reuseIdentifier: String?) {` or? – John Doe Apr 15 '16 at 07:42
  • Yes, like described in michael's answer. – vadian Apr 15 '16 at 07:43
  • 1
    PS: And you have also to initialize the `UILabel` with a frame. By the way: The naming convention suggests to use class names starting with a capital letter. – vadian Apr 15 '16 at 07:52
  • @vadian oh yes, you're right. I do not know why, but cell classes I call with lower case. I'll fix it, thanks! – John Doe Apr 15 '16 at 07:54

1 Answers1

2

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{}
TMob
  • 1,278
  • 1
  • 13
  • 33
michael wang
  • 553
  • 3
  • 11
  • 1
    Your answer isn't really in good condition. Maybe improve your sentences and code-editing. – TMob Apr 15 '16 at 07:34
  • sorry, my bad, i am not so familiar with tags of stackoverflow. i will never do this again,it is not professinal – michael wang Apr 15 '16 at 07:36
  • oh my god!!! sorry for my poor english , it is my second language,I just learn it from american dramas. I think i should be more careful about my next post.. – michael wang Apr 15 '16 at 07:44
  • 1
    Since you - the developer – have created the cell you are supposed to know that the cell definitely exists and you can forced cast the cell. – vadian Apr 15 '16 at 07:46