简体   繁体   English

如何在不重新加载图像的情况下重新加载CollectionView单元格

[英]How to reload a collectionview cell without reloading an image

I have a collection view and when something changes I update the data source and reload the cell in which the change occurs. 我有一个集合视图,当发生更改时,我会更新数据源并重新加载发生更改的单元格。 The cell kind of blinks when it reloads. 单元格类型在重新加载时闪烁。 It doesn't really affect the user scrolling and I made it almost unnoticeable with: 它并没有真正影响用户的滚动,我通过以下方法使其几乎不引人注意:

UIView.performWithoutAnimation{
     self.collectionView.reloadItemsAtIndexPaths([NSIndexPath(forItem: index, inSection: 0)])
}

This was the best I could do to make the reload less noticeable. 这是我能做的最好的事情,以使重新加载变得不那么明显。 I have a background image taking up the entire cell. 我有一张背景图片,占据了整个单元。 I think the flash that I'm seeing is this image reloading, yet I don't need it to reload because the image will never change. 我认为我看到的Flash是此图像的重新加载,但是我不需要重新加载它,因为该图像永远不会改变。 Does anyone know how to make the cell reload but not the image? 有谁知道如何重新加载单元格而不是图像? I can put a variable in there and change it such as (initalLoad = false) but I don't know how to keep the image from reloading. 我可以在其中放置一个变量并进行更改,例如(initalLoad = false),但我不知道如何防止重新加载图像。

Try moving all your cell setup to an internal func in your UICollectionViewCell subclass: 尝试将所有单元格设置移动到UICollectionViewCell子类中的内部函数中:

class MyCollectionViewCell: UICollectionViewCell {

    var initialLoad = true

    // since collection view cells are recycled for memory efficiency,
    // you'll have to reset the initialLoad variable before a cell is reused
    override func prepareForReuse() {
        initialLoad = true
    }

    internal func configureCell() {

        if initialLoad {
            // set your image here
        }

        initialLoad = false

        // do everything else here
    }

}

and then calling it from your view controller: 然后从您的视图控制器调用它:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath) as! MyCollectionViewCell
    cell.configureCell()
    return cell
}

You can add parameters to the configureCell() function to pass in any data you need for setting up the cell (presumably you'll need to pass in some kind of reference to your image). 您可以向configureCell()函数添加参数,以传递设置单元所需的任何数据(大概需要传递对图像的某种引用)。 If you have a lot of information, you might want to create a custom object to hold all that information, then pass that into the function as a parameter. 如果您有很多信息,则可能需要创建一个自定义对象来保存所有这些信息,然后将其作为参数传递给函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 下载图像后重新加载CollectionView的单元格 - Reload cell of CollectionView after image is downloaded 如何只重新加载collectionView中的一个单元格? - How to reload only one cell in a collectionView? 如何在willDisplayCell回调中重新加载collectionView的单个单元格? - How to reload a single cell of a collectionView in the willDisplayCell callback? 如何在删除单元格后重新加载 CollectionView - How to reload a CollectionView after deleting a cell 如何在不重新加载MapView的情况下重新加载MKAnnotations? - How to reload MKAnnotations without reloading MapView? 如何在不重新加载visibleCell的情况下在collectionView上重新加载数据 - How can I reloadData on collectionView without reloading visibleCell 如何从collectionView中的单元格重新加载一个项目? 斯威夫特4 - How reload only one item from a cell in a collectionView? Swift 4 如何根据单元格中的图像快速调整CollectionView单元格的高度? - How to resize CollectionView cell height according to image in cell in swift? 使用包含CollectionView的自定义单元格重新加载单个TableViewCell - Reload single TableViewCell with Custom Cell that includes CollectionView 如何在UICollectionView中重新加载单元格而不重新加载标头? - How do I reload cells in UICollectionView without reloading the header?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM