Swift Custom TableViewCell (Card)

353 Views Asked by At

I am trying to make a card-like cell (one that looks like it is floating on its own). Right now, I am just trying to get a red UIView to appear within the cell (padded by 12 pixels for the margins). Here is the code from the UITableViewCell class:

import UIKit
import PureLayout

class MovieTableViewCell: UITableViewCell {

    let cardView = UIView()

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

        var margins = layoutMargins
        margins.left = 12
        margins.top = 12
        margins.right = 12
        margins.bottom = 12
        contentView.layoutMargins = margins

        cardView.backgroundColor = UIColor.red

        contentView.addSubview(cardView)
        contentView.backgroundColor = nil
        contentView.preservesSuperviewLayoutMargins = false

        cardView.autoPinEdgesToSuperviewMargins()
    }

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

Currently I am getting a blank tableView. No red view is showing up. Anyone know the piece of the puzzle I am missing (P.S. I am using PureLayout)

1

There are 1 best solutions below

0
On

The contentView of the tableView can't and doesn't respond to margins , it takes the full width of the tableView , also you have to use auto-layout to put the redView so it stretches to it's content , or use heightForRowAt for a frame-layout