Change same UICollectionViewCell layout for different views

巧了我就是萌 提交于 2019-12-11 04:57:27

问题


I have two views, B and C, and B inherits from C.

C contains a CollectionView. Each cell is of type ProductsCollectionViewCell (Code below)

When view C loads its products, this is what I get:

In view B, I want to have the cells look like this :

in view C, I have this:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
    return GetViewCGSize(collectionView)
}

As you can see, I have used GetViewCGSize (my own method) so I can override it in other views to get different cell size.

So I used override on this function in view B:

override func GetViewCGSize(_ collectionView: UICollectionView) -> CGSize
    {
        return CGSize(width: self.ProductsCollection.frame.width, height: 10)
    }

yet the view for B stays the same as for C.

What am I missing?

This is the code for the cell:

class ProductsCollectionViewCell: UICollectionViewCell
{    
    var ProductImageView: UIImageView!
    var ProductName: UILabel!
    var ProductPrice: UILabel!

    var productUniqueID: String!

    // Initialization from here
    override init(frame: CGRect)
    {
        super.init(frame: frame)

        commonInit()
    }


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

        commonInit()
    }

    func commonInit()
    {
        ProductImageView = UIImageView()
        ProductPrice = UILabel()
        ProductName = UILabel()
        ProductImageView.translatesAutoresizingMaskIntoConstraints = false
        ProductName.translatesAutoresizingMaskIntoConstraints = false

        self.addSubview(ProductImageView)
        self.addSubview(ProductName)
        self.addSubview(ProductPrice)

        ProductImageView.image = #imageLiteral(resourceName: "DefaultProductImage")
        ProductImageView.contentMode = .scaleAspectFit

        ProductImageView.layer.borderColor = UIColor.black.cgColor
        ProductImageView.layer.borderWidth = 1
        self.layer.borderColor = UIColor.black.cgColor
        self.layer.borderWidth = 1
    }

    override func layoutSubviews()
    {
        let margins = self.contentView.layoutMarginsGuide

        //Add top, left, right constraint relative to cell content view like below
        ProductImageView.topAnchor.constraint(equalTo: margins.topAnchor,constant:5).isActive = true
        ProductImageView.centerXAnchor.constraint(equalTo: margins.centerXAnchor).isActive = true
        ProductImageView.widthAnchor.constraint(equalTo: margins.widthAnchor).isActive = true
        ProductImageView.bottomAnchor.constraint(equalTo: margins.bottomAnchor, constant: -50).isActive = true

        //Now set your label top anchor to display it below your image view
        ProductName.topAnchor.constraint(equalTo: ProductImageView.bottomAnchor,constant:5).isActive = true
        //To center align
        ProductName.centerXAnchor.constraint(equalTo: margins.centerXAnchor).isActive = true

        ProductPrice.translatesAutoresizingMaskIntoConstraints = false
        ProductPrice.topAnchor.constraint(equalTo: ProductName.bottomAnchor,constant:5).isActive = true
        ProductPrice.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
        //To center align
        ProductPrice.centerXAnchor.constraint(equalTo: margins.centerXAnchor).isActive = true
    }
}



extension UIImageView
{
    func loadImageUsingUrlString(urlString: String)
    {
        let url = NSURL(string: urlString)
        URLSession.shared.dataTask(with: url! as URL, completionHandler:
        {
            (data, response, error) in
            if error != nil
            {
                print (error!)
                return
            }

            DispatchQueue.main.async
            {
                self.image = UIImage(data: data!)
            }
        }).resume() // Closes URLSession.shared

    }
}

While I understand that the cell's content will not change as I have not changed anything to do so, I at least expect the cell itself to change size.

I do not want to use table view, but stay with my UICollectionView C as it has many needed functions.

来源:https://stackoverflow.com/questions/53681003/change-same-uicollectionviewcell-layout-for-different-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!