Reputation: 17902
I'm using story board, i have custom tableviewcell class. I dragged outlets from story board to tableviewcell. In that i'm adding corner and colours for components like this. But i'm getting this error Fatal error: Unexpectedly found nil while unwrapping an Optional value. How to solve this
My code in tableviewcell class is.
import UIKit
class detailsTableViewCell: UITableViewCell {
@IBOutlet weak var chargesTableViewCellView: UIView!
@IBOutlet weak var typeLbl: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
chargesTableViewCellView.layer.borderColor = UIColor(red: 0.10, green: 0.39, blue: 0.65, alpha: 1.0).cgColor // Here I'm getting error
chargesTableViewCellView.layer.borderWidth = 2.0
chargesTableViewCellView.layer.cornerRadius = 10.0
}
Upvotes: 0
Views: 396
Reputation: 3494
I think you have used more then one cell with single custom class so you need to add check with re-use identifier :
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
// "identifier" is a cell identifier so please change this identifier with your cell identifier witch is used this outlet
if self.reuseIdentifier == "identifier" {
chargesTableViewCellView.layer.borderColor = UIColor(red: 0.10, green: 0.39, blue: 0.65, alpha: 1.0).cgColor // Here I'm getting error
}
chargesTableViewCellView.layer.borderColor = UIColor(red: 0.10, green: 0.39, blue: 0.65, alpha: 1.0).cgColor // Here I'm getting error
chargesTableViewCellView.layer.borderWidth = 2.0
chargesTableViewCellView.layer.cornerRadius = 10.0
}
You need to add the check for reuseIdentifier
that cell contains this outlet. this will work for you.
Upvotes: 1
Reputation: 2198
Go to the Interface Builder and delete ALL of your references to the file. Select the storyboard container and click on the most right button in the Interface Builder. You will find a list of references in there. Click on (x) for each of them.
Reconnect after that.
If you changed a name, copied-pasted or moved around things in the storyboard, sometimes the references get mixed. It all looks good on Xcode, but the id
s of the references have mismatches.
Upvotes: 0
Reputation: 780
You can create a class for your custom view like this
import UIKit
class CornerView: UIView {
func setup() {
self.layer.borderColor = UIColor(red: 0.10, green: 0.39, blue: 0.65, alpha: 1.0).cgColor
self.layer.borderWidth = 2.0
self.layer.cornerRadius = 10.0
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
}
then you can inherit from your custom class
@IBOutlet weak var chargesTableViewCellView: CornerView!
notice: make sure for cell identifier from storyboard is the same in ViewController
Upvotes: 0