我有两个视图B和C,B继承自C。
C包含一个CollectionView。每个单元格的类型为ProductsCollectionViewCell
(下面的代码)
当视图C加载其产品时,这就是我得到的:
在视图B中,我希望单元格看起来像这样:
在视图C中,我有这个:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return GetViewCGSize(collectionView)
}
如您所见,我使用了GetViewCGSize
(我自己的方法),因此可以在其他视图中覆盖它以获得不同的像元大小。
所以我在视图B中使用了对此功能的覆盖:
override func GetViewCGSize(_ collectionView: UICollectionView) -> CGSize
{
return CGSize(width: self.ProductsCollection.frame.width, height: 10)
}
但是B的视图与C的视图相同。
我想念什么?
这是单元格的代码:
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
}
}
虽然我知道单元格的内容不会更改,因为我没有进行任何更改,但我至少希望单元格本身会更改大小。
我不想使用表视图,但因为它具有许多需要的功能,所以请留在我的UICollectionView
C上。