jojiReptiloid
jojiReptiloid

Reputation: 286

Add background view in collection cell

I use UITableViewCell.xib to customize the cell. I display text and pictures, View added to the background of the cells. There are only 7 cells and in one cell I need to replace this background with another UIView.xib. I tried to implement this, but it did not give me results, the background on all cells is the same. Or how can I change the property of the Background View cell to my View.xib

CollectionCell.swift

import UIKit

class CollectionViewCell: UICollectionViewCell {

    static var identifier: String = "CollectionViewCell"

    @IBOutlet weak var icon: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var customBackgoundView: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

    }
}

CustomeView.swift

import UIKit

class CustomBackgroundView: UIView {

    @IBOutlet weak var contentView:UIView!


    //MARK: Override
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    private func commonInit() {
        Bundle.main.loadNibNamed("CustomBackgroundView", owner: self, options: nil)
        addSubview(contentView)
        contentView.frame = self.bounds
    }
}

CollectionView.swift let background = CustomBackgroundView()

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.identifier, for: indexPath) as? CollectionViewCell


            // here i'm doing something wrong because nothing changes
  if indexPath.row == 3 {
        cell?.customBackgoundView = background
        cell?.contentView.addSubview(background)
    }
        return cell ?? UICollectionViewCell()
    }

Upvotes: 1

Views: 356

Answers (2)

Sweta Vani
Sweta Vani

Reputation: 61

You need to load UIView on indexPath.row == 3 as following

let myView = Bundle.main.loadNibNamed("yourXibView", owner: nil, options: nil)![0] as! UIView

then add in your collection view's background main view

Upvotes: 2

Anil Kumar
Anil Kumar

Reputation: 1984

the issue is when you want to add something over the cell it should add it's contentview. So, try using with contentView

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.identifier, for: indexPath) as? CollectionViewCell
 if indexPath.row == 3 {
                 var backgroundView = Bundle.main.loadNibNamed("CustomBackgroundView", owner: self, options: nil)![0] as! UIView
             backgroundView.frame = cell.contentView.frame
            cell.contentView.addSubview(yourCustomView)
            cell.contentView.sendSubviewToBack(yourCustomView)
        return cell!
                } else {
                    return cell ?? UICollectionViewCell()
                }

Upvotes: 2

Related Questions