17

I try to give my UITableViewCell Class a custom initilizer but i can't figure it out what I am doing wrong.

Here is my Code:

init(dataObject: [NSManagedObject]!, objectAttributeValues: [String]!,placeholder: String!, segmentedControl: UISegmentedControl?, cellHeight: CGRect, cellWidth: CGRect) {
    self.dataObject = dataObject
    self.Placeholder.text = placeholder
    self.objectAttributeValues = objectAttributeValues

    if segmentedControl != nil {
        self.segmentedControl = segmentedControl!
        didHaveSegmentedControl = true
    }

}

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

I tried to call super.init(frame: CGRect(...)) but by implementing this I get another error: Must call a designated initializer of the superclass 'UITableViewCell'

What can I do? Thank you a lot!

Tom Kuschka
  • 463
  • 1
  • 8
  • 17

3 Answers3

16

The way initialisers work, is they will add their own properties, constants and functions to that instance, then call back to the superclass for an object of it's type. More info here.

For this reason you must call a superclass' initialiser before exiting the initialiser. Here I suggest you call super.init() on the last line of your initialiser. You can choose which of the init methods on UITableViewCell is most appropriate.

vrwim
  • 13,020
  • 13
  • 63
  • 118
  • 16
    If I call the 'super.init()' the error "_Must call a designated initialiser of the superclass "UITableViewCell"_" will be appear... – Tom Kuschka Jun 17 '15 at 04:19
  • 1
    You must call one of the other initialisers, depending on what you want to do. Check the autocomplete for more. – vrwim Jun 17 '15 at 09:19
2
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    convenience init(frame: CGRect, dataObject: [NSManagedObject]!, objectAttributeValues: [String]!,placeholder: String!, segmentedControl: UISegmentedControl?, cellHeight: CGRect, cellWidth: CGRect){
        self.init(frame: frame)
        self.dataObject = dataObject
        self.Placeholder.text = placeholder
        self.objectAttributeValues = objectAttributeValues

        if segmentedControl != nil {
            self.segmentedControl = segmentedControl!
            didHaveSegmentedControl = true
        }

    }

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

I hope this will help you Override init method of UIView in swift

Mir Ashraf
  • 121
  • 7
0

You have to call the superclasses designated initializer.

For example, I tried to create a subclass of UIView and had the exact same problem you had. The designated initializer for UIView is super.init(frame: CGRect) For UITableViewCell the designated initializers are as follows.

// Designated initializer.  If the cell can be reused, you must pass in a reuse 
identifier.  You should use the same reuse identifier for all cells of the same 
form.  
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier: 
(nullable NSString *)reuseIdentifier NS_AVAILABLE_IOS(3_0) 
NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder 
NS_DESIGNATED_INITIALIZER;

Hope this helped.

Mateusz
  • 698
  • 1
  • 8
  • 18