我使用 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/