0

I have a uicollectionview inside a uitableview cell. For 2 of the tableview cells, the label inside the uicollection view cells display correct (under the images), but for one of the cells, the label does not, it displays at the top of the images.

See below - notice the labels in black. For one of the tableViewCells, the label displays above the image when I want it to display at the bottom. How can I fix this? I think this has to do with the tableView reusing cells.

enter image description here

Here's the code for the collection view cell

class BookCollectionViewCell: UICollectionViewCell {
    var imageView: UIImageView!
    var label: UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)

        imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.isUserInteractionEnabled = false
        contentView.addSubview(imageView)

        label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont(name: "CeraPro-Regular", size: 12);
        label.textColor = UIColor.black
        label.center = self.center
        label.translatesAutoresizingMaskIntoConstraints = false
        label.textAlignment = .center
        label.backgroundColor = UIColor.black
        contentView.addSubview(label)

    }

    override func layoutSubviews() {
        super.layoutSubviews()

        var lframe = label.frame
        lframe.size.height = 20
        lframe.size.width = self.frame.size.width
        lframe.origin.x = 0
        lframe.origin.y = self.frame.size.height - 20
        label.frame = lframe

        var frame = imageView.frame
        frame.size.height = self.frame.size.height - 38
        frame.size.width = self.frame.size.width
        frame.origin.x = 0
        frame.origin.y = 5
        imageView.frame = frame

        imageView.layer.shadowColor = UIColor.gray.cgColor
        imageView.layer.shadowOpacity = 0.3
        imageView.layer.shadowOffset = CGSize.zero
        imageView.layer.shadowRadius = 5
        imageView.layer.shadowOffset = CGSize(width: 4, height: 4)
        imageView.layer.masksToBounds = false
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
Pota Onasys
  • 998
  • 3
  • 8
  • 18

0 Answers0