Property 'self.delegate' not initialised at super.init - ios

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)
}
}

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).

Change
let delegate : tableCellBtnActionDelegate!
to
var delegate : tableCellBtnActionDelegate!
or you can't set value to delegate property ever

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()

Related

How to initialize customized UITableViewCell ui-related property

I am created a custom UITableViewCell and added some outlet fields which connect to the UI elements on storyboard. Please see below as an example. But I always got nil pointer exception on the line "self.skillBtn.backgroundColor = UIColor.redColor()" in "required init?(coder aDecoder: NSCoder) " method. It means that the skillBtn has not been initialized yet. I wander where I should do the UI initialization on a UITableViewCell?
#IBOutlet weak var backgroundImg: UIImageView!
#IBOutlet weak var userNameLabel: UILabel!
var delegate: UserHomeCategorySelectionDelegate?
#IBOutlet weak var skillBtn: UserHomeCategoryButton!
#IBOutlet weak var speakBtn: UserHomeCategoryButton!
#IBOutlet weak var friendsBtn: UserHomeCategoryButton!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.skillBtn.backgroundColor = UIColor.redColor()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.skillBtn.backgroundColor = UIColor.redColor()
}
Thats because the views are not yet connected from your Storyboard/XIB to your class instance,
to achieve what you want, override awakeFromNib method and put self.skillBtn.backgroundColor = UIColor.redColor() inside it.
awakeFromNib is called after view is loaded from the XIB/Storyboard and outlets are connected.

FirebaseUi - Cast value of UITableViewCell to custom class in swift

I can t seem to get the prototypereuseidentifier thing on firebaseUI. If I add a prototypecell, I can only give it a cellidentifier
In my following code, my IBoutlets linked to my custom cell return nil.
here s my code :
func loadData() {
self.dataSource = FirebaseTableViewDataSource(ref: refpost, cellClass: MainWitnessTableViewCell.self, cellReuseIdentifier: "<RIcell>", view: self.tableView)
self.dataSource.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in
let snap = obj as! FDataSnapshot
print(cell)
let mycell = cell as! MainWitnessTableViewCell
let keyy: String = (snap.value.objectForKey("author") as? String)!
mycell.postContent.text = keyy
print (mycell.postContent.text)
}
self.tableView.dataSource = self.dataSource
}
here, mycell.postContent.text returns nil, is there any sorcery that keeps blinding me ? :)
sincerely
Yann
I think you should change
cellReuseIdentifier: "<RIcell>"
to
cellReuseIdentifier: "RIcell"
# jonas : here s the cell class
class MainWitnessTableViewCell: UITableViewCell, UITextFieldDelegate {
#IBOutlet weak var cellImage: UIImageView!
#IBOutlet weak var postOwner: UILabel!
#IBOutlet weak var postDate: UILabel!
#IBOutlet weak var postContent: UITextView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
postContent.text = ""
postOwner.text = ""
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}

How do I set an initializer for a custom UITableViewCell class?

I've searched around for an answer to this, but each solution just brings new compiler errors.
How do I set up the initializers for a class that inherits from UITableViewCell?
This is what I have so far, any help would be greatly appreciated:
SettingCell.swift
class SettingCell: UITableViewCell {
#IBOutlet weak var settingsLabel: UILabel!
#IBOutlet weak var settingsSwitch: UISwitch?
#IBOutlet weak var timeSetting: UILabel?
var cellDelegate: SettingCellDelegate?
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
init(settingLabel: UILabel!, settingSwitch: UISwitch?, timeSetting: UILabel?) {
super.init(style: UITableViewCellStyle, reuseIdentifier: String?)
self.settingsLabel = settingLabel
self.settingsSwitch = settingSwitch
self.timeSetting = timeSetting
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#IBAction func handledSwitchChange(sender: UISwitch) {
self.cellDelegate?.didChangeSwitchState(sender: self, isOn:settingsSwitch!.on)
}
...
}
Fix-It Error:
Compiler Error:
'UITableViewCellStyle.Type' is not convertible to 'UITableViewCellStyle'
When you call this line:
super.init(style: UITableViewCellStyle, reuseIdentifier: String?)
you need to supply an actual cell style and an actual reuse identifier. You code at the moment is written as if this line is a function definition, not a function invocation.
Change it to something like:
super.init(style: .Default, reuseIdentifier: "SettingCell")
(though preferably at least the reuse identifier should be provided by the caller, not statically)

Swift: IBoutlets are nil in Custom Cell

I can't figure out why this custom cell is not being output.
I have a custom cell set up in a storyboard (no nib).
I have a text field and 2 labels which are nil when I try to access them in the custom cell class. I'm almost sure I have everything hooked up correctly, but still getting nil.
I've selected my table cell in the storyboard and set Custom Class to TimesheetTableViewCell
I've also control clicked on the table and set the datasource and delegate to be TimesheetViewController
My custom cell class:
import UIKit
class TimesheetTableViewCell: UITableViewCell {
#IBOutlet var duration: UITextField!
#IBOutlet var taskName: UILabel!
#IBOutlet var taskNotes: UILabel!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init?(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
println("Cell's initialised")// I see this
println(reuseIdentifier)// prints TimesheetCell
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func setCell(duration: String, taskName: String, taskNotes: String){
println("setCell called")
self.duration?.text = duration
self.taskName?.text = taskName
self.taskNotes?.text = taskNotes
}
My controller:
class TimesheetViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
#IBOutlet var timesheetTable: UITableView!
var items = ["Item 1", "Item2", "Item3", "Item4"]
override func viewDidLoad() {
super.viewDidLoad()
self.timesheetTable.registerClass(TimesheetTableViewCell.self, forCellReuseIdentifier: "TimesheetCell")
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TimesheetCell", forIndexPath: indexPath) as TimesheetTableViewCell
println(items[indexPath.row]) // prints corresponding item
println(cell.duration?.text) // prints nil
cell.setCell(items[indexPath.row], taskName: items[indexPath.row], taskNotes: items[indexPath.row])
return cell
}
The problem is this line:
self.timesheetTable.registerClass(TimesheetTableViewCell.self, forCellReuseIdentifier: "TimesheetCell")
Delete it. That line says: "Do not get the cell from the storyboard." But you do want to get the cell from the storyboard.
(Make sure, however, that the cell's identifier is "TimesheetCell" in the storyboard, or you'll crash.)
Pretty sure that you need to remove the line
self.timesheetTable.registerClass(TimesheetTableViewCell.self, forCellReuseIdentifier: "TimesheetCell")
from viewDidLoad().

iOS 8 UITableViewCell Class Compiler Error since Xcode Beta5

I'm already using Swift and since the update of Xcode beta4 to beta5 there are two compiler errors in the following code.
1st: init function - Error: "Overriding declaration requires an 'override' keyword" --> Solved
2nd: class FeedTableCell : UITableViewCell - Error: "Class 'FeedTableCell' does not implement its superclass's required members"
I couldn't find the required members in the documentation and other research - does anyone know what to do?
Code:
import UIKit
class FeedTableCell : UITableViewCell{
#IBOutlet var userLabel: UILabel!
#IBOutlet var hoopLabel: UILabel!
#IBOutlet var postLabel: UILabel!
func loadItem(#user: String, hoop: String, post: String) {
userLabel.text = user
hoopLabel.text = "#"+hoop
postLabel.text = post
}
init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool){
super.setSelected(selected, animated: animated)
}
}
I resolved this issue by adding required init(coder aDecoder: NSCoder!) method:
class FeedTableCell : UITableViewCell{
#IBOutlet var userLabel: UILabel!
#IBOutlet var hoopLabel: UILabel!
#IBOutlet var postLabel: UILabel!
func loadItem(#user: String, hoop: String, post: String) {
userLabel.text = user
hoopLabel.text = "#"+hoop
postLabel.text = post
}
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool){
super.setSelected(selected, animated: animated)
}
}
The other solution is remove init method at all:
class FeedTableCell : UITableViewCell{
#IBOutlet var userLabel: UILabel!
#IBOutlet var hoopLabel: UILabel!
#IBOutlet var postLabel: UILabel!
func loadItem(#user: String, hoop: String, post: String) {
userLabel.text = user
hoopLabel.text = "#"+hoop
postLabel.text = post
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool){
super.setSelected(selected, animated: animated)
}
}

Resources