ios - 调用 collectionView(_ :layout:sizeForItemAt:) in Swift 4

标签 ios swift xcode swift4

我使用 UICollectionView 制作了画廊。

如果你降低屏幕,图像应该继续出现并停在 20。

代码不断更新。 但是设置cell大小的函数collectionView(_:layout:sizeForItemAt:)没有起作用。 因此,只有20张图片连续显示。

在 viewDidLoad() 上

if let layoutt = artCollectionView?.collectionViewLayout as? PinterestLayout
{
    layoutt.delegate = self
}

单元格设置

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

    let arts = self.artList else { return HomeCollectionViewCell() }
    if arts.count > indexPath.row
    {
        let model = arts[indexPath.row]

        cell.imgView.sd_setImage(with: URLHelper.createEncodedURL(url: model.thumburl), completed: nil)
    }
    return cell
}

返回单元格大小

extension HomeViewController : PinterestLayoutDelegate
{
    func collectionView(_ collectionView: UICollectionView, heightForPhotoAtIndexPath indexPath:IndexPath) -> CGFloat
    {
        return CGFloat(300.0)
    }
}

目前,该函数仅在应用启动时执行一次。您知道如何在我每次需要时运行该函数吗?

最佳答案

您需要实现UICollectionViewDelegateFlowLayout。 您可以在此处阅读有关该协议(protocol)的更多信息: UICollectionViewDelegateFlowLayout protocol

示例:

extension viewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 50, height: 300)
    }
}

关于ios - 调用 collectionView(_ :layout:sizeForItemAt:) in Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53003494/

相关文章:

arrays - SWIFT - 将数组向下转换为字符串

ios - Swift 单元测试从闭包内部抛出函数

xcode -- 基于构建配置的条件编译

ios - swift 5。协议(protocol)扩展引发编译错误 'Cannot invoke function with an argument list of type Self'

ios - MKMapView 到带有叠加层的 UIImage

ios - 在 swift 5 中将字节转换为 unicode 文本

ios - 带有 subview 的 UIViews : calculating position when scaling

iphone - 我如何将 Xcode 从 4.2.1 升级到 4.3.2?

ios - 对 NSOperationQueue 工作的困惑

ios - 来自动态 UITableView 的动态转场似乎是不可能的