Reputation: 41
I am trying to create an array of UITableViewCells and before I append the cells I need to initialize them with parameters. When I try to initialize the cell I get an error that the two properties in the class were never defined. But after I defined them I get an error that the variables were used before being initialized. Here is the class
class SimpleCellClassTableViewCell: UITableViewCell {
@IBOutlet var artist: UILabel!
@IBOutlet var picture: UIImageView!
@IBOutlet var songTitle: UILabel!
@IBOutlet var sender: UILabel!
var audioFile: AnyObject? = nil
var mediaType: songType! = nil
var id: NSNumber! = nil
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func configureCell(Title title: NSString?,
File audioFile: AnyObject?,
Type mediaType: songType,
Artist artist: NSString?,
Image image: UIImage?,
Sender sender: NSString?,
ID id: NSNumber?) -> UITableViewCell {
self.audioFile = audioFile
self.mediaType = mediaType
if let newSender = sender{
self.sender.text = newSender as String
}
if let newTitle = title{
self.songTitle.text = newTitle as String
}
if let newImage = image {
self.picture.image = newImage
}
if let newArtist = artist {
self.artist.text = newArtist as String
}
if let newId = id {
self.id = newId as NSNumber
}
return self
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
And this is where Im trying to initialize and then add values to it with configure cell method:
let newSongCell = SimpleCellClassTableViewCell.init(style: .Default, reuseIdentifier: "SimpleCell")
newSongCell.configureCell(Title: setTitle,
File: setAudioFile,
Type: setMediaType,
Artist: setArtist,
Image: setImage,
Sender: setSender,
ID: setId)
The parameters for File and Type are the ones throwing the same error. Also if I need to use the initializer with NSCoder what should I put as the argument?
Upvotes: 4
Views: 4156
Reputation: 6114
I don't see if audioFile
and mediaType
properties are defined wrong. There should be no error with them. If you use .xib file - usually you should not use explicit initializer. And for sure you must not do this in your case, because you trying to use outlets. When you use init(style:reuseIdentifier:)
you miss your .xib file and all UI. Assume that .xib filename is SimpleCellClassTableViewCell.xib, Custom Class for UITableViewCell in Identity Inspector is set to SimpleCellClassTableViewCell
, reuse identifier is set to "SimpleCell". I offer you use this code for initializing your cell:
guard let newSongCell = UINib(nibName: "SimpleCellClassTableViewCell", bundle: nil).instantiateWithOwner(nil, options: nil).first as? SimpleCellClassTableViewCell
else
{
fatalError("Error loading SimpleCellClassTableViewCell")
}
Usually you should not use initializer with NSCoder explicitly. This initializer used by storyboard or .nib file.
At last, if you use outlets from storyboard - then you shouldn't try to get cell instance by any initializer at all. You should use UITableView
's dequeueReusableCellWithIdentifier(forIndexPath:)
method.
That was all about your direct question, but in fact you rarely need to initialize UITableViewCell
by yourself. If you do, probably you do something wrong. Assume again, that you use .xib file, then in most cases you simply register your file in viewDidLoad
of your table view controller
tableView.registerNib(UINib(nibName: "SimpleCellClassTableViewCell", bundle: nil), forCellReuseIdentifier: "SimpleCell")
and then your tableView
will initialize or reuse SimpleCellClassTableViewCell
instances for you by dequeueReusableCellWithIdentifier(forIndexPath:)
method
Upvotes: 1
Reputation: 4733
There are several problems with your code. I suggest you to look some examples where UITableView
. Apple docs or github are good sources for this.
Issue #1
You do not need to override designated `UITableViewCell's initializers (below) because you do nothing in the override.
public init(style: UITableViewCellStyle, reuseIdentifier: String?)
public init?(coder aDecoder: NSCoder)
Issue #2
Your code does not reuse the cell object which is bad.
Issue #3
In Swift init
is not used on the call site, so your cell's initialization code (considering also issue #2) should be
var simpleCell = tableView.dequeueReusableCellWithIdentifier("SimpleCell")
if simpleCell == nil {
let cell: SimpleCellClassTableViewCell = SimpleCellClassTableViewCell(style: .Default, reuseIdentifier: "SimpleCell")
cell.configureCell(Title: "test",
File: "",
Type: 2,
Artist: "test",
Image: UIImage(),
Sender: "test",
ID: 2)
simpleCell = cell
}
return simpleCell!
Issue #4
Do not name function parameters with capitalised first letter (Title
, File
, etcetera). This might be confused with a type. Instead, use title
, file
, etc.. Again, there are a lot of examples out there.
Try to fix this issues. This might help.
Upvotes: 1