【问题标题】:Preload Collection View (So that scrolling is smooth)预加载集合视图(使滚动流畅)
【发布时间】:2018-11-05 17:14:54
【问题描述】:

我希望能够在我的收藏视图上下滚动时预加载项目。

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

(1)availableLabelNames: [String] (2)availableImageNames: [String]

AvailableImages 中的集合视图函数:

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

自定义集合视图类:

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

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

【问题讨论】:

    标签: ios swift uicollectionview collectionview preloading


    【解决方案1】:

    集合视图默认进行预加载。您的问题在其他地方,您不能在单元格中为项目做繁重的工作。

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

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

    此外,由于单元格被重复使用,您希望重置其图像。否则图片会随机显示。

    【讨论】:

    • 谢谢 - 我已经实现了这个。但是,我不完全确定,但它似乎使它变得更慢。在重用单元格时想要“重置他们的图像”是什么意思 - 我该如何实现这一点。最后,有什么方法可以从另一个视图控制器预加载这个集合视图。再次感谢!
    • 预先将图像设置为零。不,您不能从其他视图控制器预加载集合视图。问题可能是你有大图像,我的代码将消除滚动的口吃,但它不会缩短加载图像所需的时间。如果您坚持预加载,您可以预先创建单元格,但我不认为这是一个好的做法,因为可能存在内存问题。更好的做法是使单元格的图像更小。同样,这可能是也可能不是问题,因为我们看不到您的完整代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 2016-02-23
    • 1970-01-01
    • 2023-04-04
    • 2018-01-18
    • 1970-01-01
    相关资源
    最近更新 更多