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

标签 ios swift uicollectionview collectionview preloading

我希望能够在上下滚动时预加载我的 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/

相关文章:

ios - UICollectionView 水平滚动不滚动到最后一项

ios - 如何将汉堡包图标添加到 UICollectionViewCell 的右侧

ios - 当我点击任何 Collection View 单元格(不点击最后一个单元格)时,如何移动另一个 View Controller ?

ios - 在 App 开始时在 iOS 中获取位置

ios - 如何将图像拉伸(stretch)成自定义形状(swift3)

iphone - MPMoviePlayer 锁屏播放/暂停音频

swift - 使用按钮重置 UNTimeIntervalNotificationTrigger

使用 Parse 平台的 iOS 推送通知 - didRegisterForRemoteNotificationsWithDeviceToken - 从未调用

ios - Swift/XCode 编译 Archive 一个非常小的 App 需要 25 分钟

ios - 获取包含在 UIStackView 中的 UIView 的中心