gpt4 book ai didi

ios - 预加载 Collection View (以便滚动平滑)

转载 作者:行者123 更新时间:2023-11-30 11:02:33 30 4
gpt4 key购买 nike

我希望能够在上下滚动时预加载我的 Collection View 中的项目。

我使用两个数组的数据来加载 Collection View 中的数据:

(1) 可用标签名称:[字符串](2) availableImageNames: [String]

AvailableImages 中的 Collection View 函数:

class availableImages: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource
{

@IBOutlet weak var searchBar: UISearchBar!

@IBOutlet weak var collectionView: UICollectionView!

public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return availableLabelNames.count
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! CustomCollectionViewCell
cell.borderFolder.layer.borderColor = UIColor.black.cgColor
cell.borderFolder.layer.borderWidth = 2
cell.borderFolder.layer.cornerRadius = 5

cell.squareDesign.layer.cornerRadius = 5

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(availableImages.connected(_:)))
cell.imageCell.isUserInteractionEnabled = true
cell.imageCell.tag = indexPath.row
cell.imageCell.addGestureRecognizer(tapGestureRecognizer)

//setting image
cell.imageCell.image = UIImage(named: availableImageNames[indexPath.row])

//setting label
cell.labelName.text = availableLabelNames[indexPath.row]

return cell
}
}

自定义 Collection View 类:

import UIKit

protocol Normal: class
{
func delete (cell: CustomCollectionViewCell)

func pressed (cell: CustomCollectionViewCell)

func textChanged (cell: CustomCollectionViewCell)

func finishedEditing (cell: CustomCollectionViewCell)

func textStartedEditing (cell: CustomCollectionViewCell)
}

class CustomCollectionViewCell: UICollectionViewCell
{
@IBOutlet weak var imageCell: UIImageView!
@IBOutlet weak var deleteButton: UIButton!
@IBOutlet weak var cellButton: UIButton!
@IBOutlet weak var labelName: UILabel!
@IBOutlet weak var textFieldName: UITextField!
@IBOutlet weak var squareDesign: UIView!
@IBOutlet weak var borderFolder: UIView!

weak var delegate: Normal?

//...class goes on but not important

请有人帮助我,我已经到处寻找,但似乎没有任何效果或对我的申请有意义。太感谢了! :)

最佳答案

Collection View 默认进行预加载。您的问题在其他地方,您无法在单元格中为项目做繁重的工作。

需要一个示例项目来尝试它,但我敢打赌这就是问题的根源:

cell.imageCell.image = UIImage(命名:availableImageNames[indexPath.row])

将其重写为:

DispatchQueue.main.async { [weak self] in
guard let weakSelf = self else { return }
cell.imageCell.image = UIImage(named: weakSelf.availableImageNames[indexPath.row])
}

此外,当单元格被重复使用时,您需要重置它们的图像。否则,图像将随机显示。

关于ios - 预加载 Collection View (以便滚动平滑),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53159103/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com