0

I have read the following articles:

  1. creating custom tableview cells in swift
  2. 'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell'`
  3. Custom UITableViewCell from nib in Swift

However, none of them have solved my problem.

What I am trying to accomplish:

I want to dynamically create buttons in a tableview cell like as described in this post. So that I can create as many buttons I want to in a tableview cell.

The problem:

However, I keep getting the error fatal error: init(coder:) has not been implemented.

What I have done so far:

In my tableview cell class I have the following code:

var cellButton: UIButton!

init(title: String) {
    super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

    cellButton = UIButton(frame: CGRectMake(5, 5, 50, 30))
    cellButton.setTitle(title, forState: UIControlState.Normal)

    addSubview(cellButton)

}

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

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

override func awakeFromNib() {
    super.awakeFromNib()

}

In my tableview class I have written the following code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = OfficeConnectUserTableViewCell(title: "hello")
    return cell
}

Any suggestions to get rid of the error and dynamically create the button inside the cell programmatically?

Community
  • 1
  • 1
aejhyun
  • 612
  • 1
  • 6
  • 19
  • 1
    http://stackoverflow.com/questions/24036393/fatal-error-use-of-unimplemented-initializer-initcoder-for-class – Bhavin Bhadani Apr 23 '16 at 06:41
  • @El Captain I tried removing override func awakeFromNib() as that post suggested. However, the error still remains. – aejhyun Apr 23 '16 at 06:48
  • @Nitin Gohel Hm... It seems to me that you guys are seeing something that I am failing to see. If someone could please explain how the post answers my question, that would be awesome. – aejhyun Apr 23 '16 at 07:04

0 Answers0