[英]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.