在 Swift 4 中调用 collectionView(_ :layout:sizeForItemAt:) [英] Call collectionView(_ :layout:sizeForItemAt:) in Swift 4

查看:54
本文介绍了在 Swift 4 中调用 collectionView(_ :layout:sizeForItemAt:)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 UICollectionView 制作了图库.

I made the gallery using UICollectionView.

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

If you lower the screen, the image should continue to come out and stop at 20.

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

The code was constantly updated. However, the function collectionView (_: layout: sizeForItemAt :) that sets the size of the cell did not work. Therefore, only 20 images are displayed continuously.

在 viewDidLoad() 上

On 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)
    }
}

目前,该函数仅在应用启动时执行一次.你知道如何在我每次想要的时候运行那个函数吗?

Currently, the function only executes once when the app starts. Do you know how to run that function every time I want?

推荐答案

请按照下面的UICollectionView中的PinterestLayout步骤安装

Please follow the steps to install PinterestLayout in UICollectionView below.

PinterestLayout 设置为您的集合视图.

Set PinterestLayout to your collection view.

let layout = PinterestLayout()
collectionView.collectionViewLayout = layout

设置布局

layout.delegate = self
layout.cellPadding = 5
layout.numberOfColumns = 2

实现PinterestLayoutDelegate

extension CustomCollectionVC: PinterestLayoutDelegate {

    func collectionView(collectionView: UICollectionView,
                        heightForImageAtIndexPath indexPath: IndexPath,
                        withWidth: CGFloat) -> CGFloat {
        let image = images[indexPath.item]

        return image.height(forWidth: withWidth)
    }

    func collectionView(collectionView: UICollectionView,
                        heightForAnnotationAtIndexPath indexPath: IndexPath,
                        withWidth: CGFloat) -> CGFloat {
        let textFont = UIFont(name: "Arial-ItalicMT", size: 11)!                
        return "Some text".heightForWidth(width: withWidth, font: textFont)
    }
}

加载数据时布局无效,并在集合视图上重新加载数据.

When data is loaded invalidate layout as well as reload data on collection view.

collectionView.collectionViewLayout.invalidateLayout()
collectionView.reloadData()

观察:

func collectionView(_ collectionView: UICollectionView, heightForPhotoAtIndexPath indexPath:IndexPath) -> CGFloat

试试这个而不是上面的:

try this instead of above:

func collectionView(collectionView: UICollectionView,
                        heightForImageAtIndexPath indexPath: IndexPath,
                        withWidth: CGFloat) -> CGFloat

这篇关于在 Swift 4 中调用 collectionView(_ :layout:sizeForItemAt:)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆