UICollectionView动态单元格高度(Pinterest布局)

10

我正在尝试在我的应用程序中实现Pinterest的布局

我使用了以下委托方法来实现这一点,并动态设置单元格高度

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let boundingRect =  CGRect(x: 0, y: 0, width: self.view.frame.size.width / 2 - 20, height: CGFloat(MAXFLOAT))
    let data = articles[indexPath.row]
    let rect  = AVMakeRect(aspectRatio: (data.coverImage?.size)!, insideRect: boundingRect)
    return CGSize(width: rect.width, height: rect.height + 120) // Added 120 for a description I will add later 
}

但我得到了以下结果:

Here

灰色背景是单元格的背景,您可以看到每个单元格都有自己的高度(正如预期的那样),但是有额外的白色空间,我不知道如何去除。

我该如何解决这个问题?


有人尝试过这个吗?为什么没有答案呢? - adev
你找到任何解决方案了吗? - H S W
3个回答

1
您可以查看raywenderlich的教程:

https://www.raywenderlich.com/164608/uicollectionview-custom-layout-tutorial-pinterest-2

如果你想在到达最后一个单元格时加载新的数据,那么你需要编辑一些代码。
在PinterestLayout.swift中,进行以下更改:
 guard cache.isEmpty == true, let collectionView =   collectionView else {
        return
    }

 guard let collectionView = collectionView else {
        return
    } 

// 这个文件名可能与您的不同

在 PhotoStreamViewController.swift 文件中,添加以下代码:

   override func collectionView(_ collectionView:  UICollectionView, willDisplay cell: UICollectionViewCell,  forItemAt indexPath: IndexPath) {
      if (indexPath.row == photos.count - 1 ) {
     //it's your last cell

     // Add more data to the array according your requirement

     // After adding data reload collection view
        collectionView.reloadData()
        
    }
}

0

你的 UICollectionView 布局不正确。从 storyboard 或通过 CollectionViewDelegateFlowLayout 方法中删除单元格间距和列间距。

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return CGFloat()// Your Value
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
     return CGFloat()// Your Value
    }

0

在我的ChatMessageController中,每个单元格的高度可能不同,以下是它的工作原理

首先,我们需要使用UICollectionViewFlowLayout。
扩展ChatMessageController { func createLayout() -> UICollectionViewLayout { let layout: UICollectionViewFlowLayout = { let layout = UICollectionViewFlowLayout() let width = UIScreen.main.bounds.size.width layout.estimatedItemSize = CGSize(width: width, height: 80) return layout }()
return layout } }
然后在UICollectionViewCell中覆盖systemLayoutSizeFitting方法,如下所示:
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize { width.constant = bounds.size.width return contentView.systemLayoutSizeFitting(CGSize(width: targetSize.width, height: getHeight())) }

where getHeight 是一个函数,根据内容计算实际单元格高度的例子:

    private func getHeight() -> CGFloat {
            let text = label.text ?? ""
            
            let constraintRect = CGSize(width: 0.75 * contentView.frame.width,
                                        height: .greatestFiniteMagnitude)
            let boundingBox = text.boundingRect(with: constraintRect,
                                                options: .usesLineFragmentOrigin,
                                                attributes: [.font: label.font as Any],
                                                context: nil)
            
            let bubbleSize = CGSize(width: boundingBox.width + d.w(30), height: boundingBox.height + d.h(30))
            
            let height = bubbleSize.height
            
            return height
        

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接