user5744323 user5744323 - 2 months ago 5x
iOS Question

How do i set width to Custom UITableViewCell

I want to set the width of custom UITableViewCell to 80% of the screen width but I know the size of the cell can be manipulated using "layoutSubviews()" methods but I don't know how.

Please help me.


The answer is too simple. Just change the frame of the cell in class.

class CustomCell: UITableViewCell {

    override var frame: CGRect {
        get {
            return super.frame
        set (newFrame) {
            var frame = newFrame
            let newWidth = frame.width * 0.80 // get 80% width here
            let space = (frame.width - newWidth) / 2
            frame.size.width = newWidth
            frame.origin.x += space

            super.frame = frame