user3576580
user3576580

Reputation: 32

Edit Style of UITableViewCell

I'd like to add a new function to my MyTableView.swift file, but i get an error:

Overriding method with selector 'initWithStyle: reuseIdentifier:' has incompatible type '(UITableViewCellStyle, String) -> MyTableViewCell'

This is the code, which i like to add:

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

Whole code MyTableView.swift file:

import UIKit

class MyTableViewCell: UITableViewCell {

    let medColor: UIColor = UIColor(red: 0.973, green: 0.388, blue: 0.173, alpha: 1)

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

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

Upvotes: 0

Views: 269

Answers (1)

Kostiantyn Koval
Kostiantyn Koval

Reputation: 8483

It says that (UITableViewCellStyle, String) type is not equal to (UITableViewCellStyle, String?)
If you look at init method of UItableViewCell, it looks like this -
init(style: UITableViewCellStyle, reuseIdentifier: String?)

Fix
Chane reuseIdentifier type for String to an optional string String?

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

Upvotes: 1

Related Questions