无法理解在集合视图中恢复单元格

时间:2015-06-29 20:04:04

标签: ios swift uicollectionview

我正在关注一个网站以帮助学习swift,我在这里对此部分感到困惑。基本上我们在集合视图加载时添加了if cell.imageview.image == nil语句,因此滚动图像时不会重新加载过滤器。我不明白的是,如果你向下滚动一个单元格被重新用于底行,那么为什么如果我向后滚动它并不是必须重新加载过滤器?数据是否保存在某处,所以当我向上滚动属性时,不必重新填充?如果是这样的话,为什么我必须使用if语句?

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! FilterCell

    if cell.imageView.image == nil {
        cell.imageView.image = placeholder

        let filterQueue: dispatch_queue_t = dispatch_queue_create("filter queue", nil)

        dispatch_async(filterQueue, { () -> Void in
            let filterImage = self.filteredImageFromImage(self.thisFeeditem.thumbNail, filter: self.filters[indexPath.row])

            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                cell.imageView.image = filterImage
            })
        })
    }
    return cell
}

2 个答案:

答案 0 :(得分:1)

重复使用单元格时,只会再次使用已分配的单元格对象。设置的任何属性或数据都将保留。

向上滚动时,单元格已经设置了图像,因此不会重新加载新的过滤图像。

答案 1 :(得分:0)

无论向上还是向下滚动,都可以重复使用单元格。您应该假设返回的单元格是不同项目的缓存版本。因此,它可能已经与另一个单元格的数据绑定,并且您希望始终使用正确的项目数据重新绑定单元格。

相关问题