簡體   English   中英

如何在不重新加載圖像的情況下重新加載CollectionView單元格

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

我有一個集合視圖,當發生更改時,我會更新數據源並重新加載發生更改的單元格。 單元格類型在重新加載時閃爍。 它並沒有真正影響用戶的滾動,我通過以下方法使其幾乎不引人注意:

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

這是我能做的最好的事情,以使重新加載變得不那么明顯。 我有一張背景圖片,占據了整個單元。 我認為我看到的Flash是此圖像的重新加載,但是我不需要重新加載它,因為該圖像永遠不會改變。 有誰知道如何重新加載單元格而不是圖像? 我可以在其中放置一個變量並進行更改,例如(initalLoad = false),但我不知道如何防止重新加載圖像。

嘗試將所有單元格設置移動到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
    }

}

然后從您的視圖控制器調用它:

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

您可以向configureCell()函數添加參數,以傳遞設置單元所需的任何數據(大概需要傳遞對圖像的某種引用)。 如果您有很多信息,則可能需要創建一個自定義對象來保存所有這些信息,然后將其作為參數傳遞給函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM