Reputation: 1721
I need to create a custom UITableViewCell, I have created a class inside of a ViewController class.
I have written the code below;
class FavoritesCell: UITableViewCell
{
var timeLabel:UILabel?
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
timeLabel = UILabel(frame: CGRectMake(250, 10, 70, 20))
//how to add this time label as a subview of FavoritesCell
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
As i mentioned in commented line, how to add timeLabel to UITableViewCell?
Thanks
Upvotes: 0
Views: 271
Reputation: 864
As UITableViewCell class is derived from the UIView, so you can use the same method - self.addSubview(timeLabel!).
Upvotes: 0
Reputation: 4544
After you've created your timeLabel
, this should do it:
self.contentView.addSubview(timeLabel!)
This adds the timeLabel
to the cell's contentView
rather than the cell itself, but that's generally what you do in table view cells.
Other things to note:
self
here - after your super.init
call, the object is fully initialised.timeLabel
, so it's safe to force-unwrap it in addSubview
.Upvotes: 1