erdemgc
erdemgc

Reputation: 1721

Custom UITableViewCell on Swift

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

Answers (2)

R_Developer
R_Developer

Reputation: 864

As UITableViewCell class is derived from the UIView, so you can use the same method - self.addSubview(timeLabel!).

Upvotes: 0

stefandouganhyde
stefandouganhyde

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:

  • It's fine to use self here - after your super.init call, the object is fully initialised.
  • You've just created the timeLabel, so it's safe to force-unwrap it in addSubview.

Upvotes: 1

Related Questions