简体   繁体   English

UICollectionView中的动态单元格高度

[英]Dynamic cell height in UICollectionView

I'm having a custom UICollectionViewCell which contains many objects but overall, it needs to connect to the database, grab stored image urls and populate a UIImageView by appending each photo (the number of photos is optional - from 1 to 10) to a UIStackView . 我有一个自定义UICollectionViewCell其中包含许多对象,但总体而言,它需要连接到数据库,抢存储的图像URL和填充UIImageView通过附加每张照片(照片数量是可选的-从1到10)的UIStackView

Now, I get everything except that the height of the cell is hard coded, so, it's not working. 现在,除了单元格的高度是硬编码的以外,我已经得到了所有东西,所以它不起作用。 I'm not sure how to get the size though. 我不确定如何获取大小。 Here's my code. 这是我的代码。

This is where the height is hardcoded. 这是高度被硬编码的地方。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,   sizeForItemAt indexPath: IndexPath) -> CGSize
{
    let width = collectionView.bounds.width
    let size = CGSize(width: width, height: 800)
    return size
}

This is where the UIImageView is being setup. 这是设置UIImageView地方。 photoURLs is the array of the downloaded URLs from the database. photoURLs是从数据库下载的URL的数组。

if let photoURLString = post?.photoURLs
{
    for urlString in photoURLString
    {
       let photoURL = URL(string: urlString)
       let imageView = UIImageView()
       imageView.sd_setImage(with: photoURL)
       imageView.contentMode = .scaleAspectFill
       imageView.clipsToBounds = true
       imageView.widthAnchor.constraint(equalTo: postImagesStackView.widthAnchor)
       postImagesStackView.addArrangedSubview(imageView)
       postImagesStackView.layoutIfNeeded()
     }
}

Also, how to leave a little bit of space between each photo? 另外,如何在每张照片之间留一点空间? Like 1 point? 喜欢1点吗?

I think that you need to calculate cell height. 我认为您需要计算像元高度。 In this case, sum images height before you call reload collectionView, or fix imageView height and multiplies images count with height. 在这种情况下,请在调用重新加载collectionView之前对图像高度求和,或者修复imageView高度,然后将图像计数乘以高度。

Another solution is make cell for each image. 另一种解决方案是为每个图像制作单元格。 Instead of stackview, you could using section and custom collectionview layout. 可以使用section和自定义collectionview布局来代替stackview。

Like woosiki indicated, you could try just calculating a value based on image sizes returned. 如woosiki所示,您可以尝试仅根据返回的图像大小计算一个值。 Once the you get the response and compute the full height, then call collectionView.reloadData() and reference that fullHeight property for each backing object. 获得响应并计算完整高度后,请调用collectionView.reloadData()并为每个后备对象引用该fullHeight属性。 Something like below, though make it safer with if let/where and default/max height. 如下所示,不过如果使用let / where和default / max height可以使其更安全。

func collectionView(_ collectionView: UICollectionView, layout 
collectionViewLayout: UICollectionViewLayout,   sizeForItemAt 
indexPath: IndexPath) -> CGSize
{
    let imageObject = dataSource.items[indexPath.row]
    let width = collectionView.bounds.width
    let size = CGSize(width: width, height: imageObject.fullHeight)
    return size
}

For a border/spacing, could include slightly larger parent view if you end up including captions or metadata anyway or just try documentation example: 对于边框/间距,如果最终还是包含标题或元数据,或者尝试使用文档示例,则可以包含稍大的父视图。

https://developer.apple.com/documentation/uikit/nslayoutanchor https://developer.apple.com/documentation/uikit/nslayoutanchor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM