CroiSciento CroiSciento - 3 months ago 28
Swift Question

Accessory Type in UITableViewCell subclass is not visible

I'm doing everything programmatically. I have a UITableView with its rows being a UITableViewCell subclass. I have been unable to set the accessoryType of my cell as nothing appears on the screen. Later on I want to be able to select a cell, and add a checkmark when selected.

class ContactsTableViewCell: UITableViewCell {

let name = UILabel()
let phoneNumber = UILabel()
let iPADFACTOR:CGFloat = DeviceHelper().isIPAD ? 1.4: 1.0

var rowSelected = false

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

phoneNumber.font = UIFont.systemFontOfSize(15)
phoneNumber.textColor = UIColor.lightGrayColor()
self.contentView.addSubview(name)
self.contentView.addSubview(phoneNumber)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {

let margeV:CGFloat = 5.0*iPADFACTOR
let margeH = self.frame.width*0.05

name.frame = CGRectMake(margeH, margeV, self.frame.width - margeH * 4, 18)
phoneNumber.frame = CGRectMake(margeH, name.frame.maxY + 5, self.frame.width - margeH * 4, 15)
}

}


As you can see I'm using the
layoutSubviews()
method, to set the frame of my labels and I've tried playing with the label size to check if somehow it was hiding the accessoryView without success. I've even tried setting their frame to 0.

I'm also not sure if I'm setting my frames correctly. Should I use
self.contentView.frame
instead of
self.frame
?

Any help is appreciated, thanks.

Answer
override func layoutSubviews() {
    super.layoutSubviews()
    let margeV:CGFloat = 5.0*iPADFACTOR
    let margeH = self.frame.width*0.05

    name.frame = CGRectMake(margeH, margeV, self.frame.width - margeH * 4, 18)
    phoneNumber.frame = CGRectMake(margeH, name.frame.maxY + 5, self.frame.width - margeH * 4, 15)
}