预加载收藏集视图(以便平滑滚动)

时间:2018-11-05 17:14:54

标签: ios swift uicollectionview collectionview preloading

当我上下滚动时,我希望能够预加载我集合视图中的项目。

我正在使用两个数组的数据将数据加载到集合视图中:

(1)availableImageNames: [String] (2)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 } }

AvailableImages中的“收藏夹视图”功能:

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

自定义集合视图类:

{{1}}

请有人帮助我,我到处都看过了,但是对于我的应用而言,似乎没有任何作用或没有意义。 非常感谢! :)

1 个答案:

答案 0 :(得分:0)

默认情况下,Collection视图正在进行预加载。您的问题在别处,您无法在单元格中完成繁重的工作。

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

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

将其重写为此:

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

此外,随着单元的重用,您想重置其图像。否则,图像将随机显示。