Reputation: 1891
My program keeps crashing in the cellForRowAtIndexPath
method when I am trying to create a custom table cell. I have put a breakpoint on each line in the code, and the code fails after I try creating the cell variable with the error: unexpectedly found nil while unwrapping optional value
. I have used custom table cells successfully many times before and have not run into this issue. It seems simple but I can't figure out what is wrong. Perhaps something in xcode changed that I'm unaware of? Any help is appreciated. Thanks!... Also, I did in fact register my custom cell in viewDidLoad
. I'll include that code as well.
var nib = UINib(nibName: "FeedTableViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "feedCell")
Probem Code Below:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:FeedTableViewCell = tableView.dequeueReusableCellWithIdentifier("feedCell", forIndexPath: indexPath) as! FeedTableViewCell
cell.descriptionLabel.text = "Testing the description label of my cell."
return cell
}
Update
The one difference between this project and others that have worked is that Xcode is now prompting me to put a bang (!) after as, whereas before I never used one. If I don't add (!) it gives me the error message "AnyObject is not convertible to FeedTableViewCell
"
as! FeedTableViewCell
-instead of- as FeedTableViewCell
FeedTableViewCell Class
import UIKit
class FeedTableViewCell: UITableViewCell {
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
priceLabel.font = UIFont(name: "AvenirNext-Bold", size: 30.0)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Upvotes: 0
Views: 2708
Reputation: 1891
After spending way too much time trying to get it to work. I nuked my custom table cell, created a brand new one, set it up the exact same way, and everything is working fine now. Still have no idea what happened, but I guess the lesson from this is that sometimes you just have to nuke it and start over.
Upvotes: 6
Reputation: 3016
you should used like this
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("feedCell", forIndexPath: indexPath) as FeedTableViewCell
cell.descriptionLabel.text = "Testing the description label of my cell."
return cell
}
for more help used this link http://shrikar.com/blog/2015/01/17/uitableview-and-uitableviewcell-customization-in-swift/
Upvotes: 0
Reputation: 3389
You should use
var cell:FeedTableViewCell = tableView.dequeueReusableCellWithIdentifier("feedCell",) as! FeedTableViewCell
NSIndexPath
may make this issue.
HTH, Enjoy Coding !!
Upvotes: -1