I have a problem with Swift class. I have a swift file for UITableViewController class and UITableViewCell class. My problem is the UITableViewCell class, and outlets. This class has an error Class "HomeCell" has no initializers, and I don't understand this problem.
Thanks for your responses.
import Foundation
import UIKit
class HomeTable: UITableViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableViex: UITableView!
var items: [(String, String, String)] = [
("Test", "123", "1.jpeg"),
("Test2", "236", "2.jpeg"),
("Test3", "678", "3.jpeg")
]
override func viewDidLoad() {
super.viewDidLoad()
var nib = UINib(nibName: "HomeCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "bookCell")
}
// Number row
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
// Style Cell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("bookCell") as UITableViewCell
// Style here
return cell
}
// Select row
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Select
}
}
// PROBLEM HERE
class HomeCell : UITableViewCell {
@IBOutlet var imgBook: UIImageView
@IBOutlet var titleBook: UILabel
@IBOutlet var pageBook: UILabel
func loadItem(#title: String, page: String, image:String) {
titleBook.text = title
pageBook.text = page
imgBook.image = UIImage(named: image)
}
}
This question is related to
ios
uitableview
swift
Quick fix - make sure all variables which do not get initialized when they are created (eg var num : Int?
vs var num = 5
) have either a ?
or !
.
Long answer (reccomended) - read the doc as per mprivat suggests...
simply provide the init block for HomeCell class
it's work in my case
My answer addresses the error in general and not the exact code of the OP. No answer mentioned this note so I just thought I add it.
The code below would also generate the same error:
class Actor {
let agent : String? // BAD! // Its value is set to nil, and will always be nil and that's stupid so Xcode is saying not-accepted.
// Technically speaking you have a way around it, you can help the compiler and enforce your value as a constant. See Option3
}
Others mentioned that Either you create initializers or you make them optional types, using ! or ? which is correct. However if you have an optional member/property, that optional should be mutable ie var
. If you make a let
then it would never be able to get out of its nil
state. That's bad!
So the correct way of writing it is:
class Actor {
var agent : String? // It's defaulted to `nil`, but also has a chance so it later can be set to something different || GOOD!
}
Or you can write it as:
class Actor {
let agent : String? // It's value isn't set to nil, but has an initializer || GOOD!
init (agent: String?){
self.agent = agent // it has a chance so its value can be set!
}
}
or default it to any value (including nil
which is kinda stupid)
class Actor {
let agent : String? = nil // very useless, but doable.
let company: String? = "Universal"
}
If you are curious as to why let
(contrary to var
) isn't initialized to nil
then read here and here
Not a specific answer to your question but I had got this error when I hadn't set an initial value for an enum while declaring it as a property. I assigned a initial value to the enum to resolve this error. Posting here as it might help someone.
This is from Apple doc
Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.
You get the error message Class "HomeCell" has no initializers because your variables is in an indeterminate state. Either you create initializers or you make them optional types, using ! or ?
In my case I have declared a Bool
like this:
var isActivityOpen: Bool
i.e. I declared it without unwrapping so, This is how I solved the (no initializer) error :
var isActivityOpen: Bool!
Source: Stackoverflow.com