Reputation: 9945
I am trying to make a protocol in a UITableViewCell
class but when i declare my delegate, I get an error in both required init?(coder aDecoder: NSCoder)
and override init(style: UITableViewCellStyle, reuseIdentifier: String?)
Error : - Property 'self.delegate' not initialised at super.init
This is my subclass:-
import UIKit
protocol tableCellBtnActionDelegate{
func removeRowAtIndex(_: Int)
}
class FriendsListTableViewCell: UITableViewCell {
@IBOutlet weak var friendListAddBtn: UIButton!
var usersId : String!
var buttonText : String!
var indexPath : Int!
let delegate : tableCellBtnActionDelegate!
override func awakeFromNib() {
super.awakeFromNib()
friendListAddBtn.layer.cornerRadius = 4
friendListAddBtn.layer.backgroundColor = UIColor(red: 121.0/255.0, green: 220.0/255.0, blue: 1, alpha: 1).CGColor
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
}
Upvotes: 0
Views: 919
Reputation: 467
You got a warning because you are not initialising the delegate
property. It should actually be a weak property, or you will retain a reference to the delegate object, which you usually don't want to do. So the best approach would be:
weak var delegate : tableCellBtnActionDelegate?
And then you have to use it like this:
self.delegate?.notifyAboutSomething()
Upvotes: 1
Reputation: 930
Well you haven't initialize it, so the compiler gives you a warning. I would advise you to modify the delegate to be optional and set your delegate whenever you need it.
var delegate : tableCellBtnActionDelegate?
You should also handle the case where delegate is not set(nil).
Upvotes: 2
Reputation: 6114
Change
let delegate : tableCellBtnActionDelegate!
to
var delegate : tableCellBtnActionDelegate!
or you can't set value to delegate property ever
Upvotes: 1