gpt4 book ai didi

ios - UICollectionView CustomCell 重用

转载 作者:行者123 更新时间:2023-11-29 01:27:09 25 4
gpt4 key购买 nike

我正在获取用户的照片资源并尝试在我的UICollectionView中创建自定义UICollectionViewCell,其中第一个索引是相机Image 和其他单元格是相机图像。但是当我滚动时,就像重新创建了索引处的图像,并且我注意到单元格中的某些图像获得了我在 index[0]

添加的 View
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

//Deque an GridViewCell
let cell: GridViewCell = (collectionView.dequeueReusableCellWithReuseIdentifier(CellReuseIdentifier, forIndexPath: indexPath) as? GridViewCell)!
if indexPath.item != 0 {

let asset = assetsFetchResults[indexPath.item - 1] as! PHAsset
cell.representedAssetIdentifier = asset.localIdentifier

imageManager.requestImageForAsset(asset, targetSize: AssetGridThumbnailSize, contentMode: PHImageContentMode.AspectFill, options: nil) { (result: UIImage?, info: [NSObject : AnyObject]?) -> Void in
//print(result)
if cell.representedAssetIdentifier.isEqualToString(asset.localIdentifier) {
if let imageResult = result {
cell.imageView.image = imageResult
}
}
}
} else {
let cameraButton = UIButton(frame: CGRectMake(0,0,80,80))
cameraButton.setTitle("Camera", forState: .Normal)
cameraButton.frame = cell.imageView.bounds
cell.imageView.addSubview(cameraButton)
}

return cell
}

下面是一张图片用于说明 Sequesnce Of Errors

最佳答案

可重用的单元格在滚动到 View 外时会被 Collection View 重用,以防止大量内存使用。当某个单元格滚动到 View 之外时, Collection View 会将它们标记为可重用。可重用的单元格将被 Collection View 重用,而不是创建新的单元格。这将显着减少内存使用。如果屏幕上同时只能容纳 20 个单元格,为什么还要在内存中保留 1000 个单元格。

因为单元格被重用,它们仍将包含前一个单元格的内容,这意味着该单元格仍具有 cameraButton 作为 subview 。与图像相同的故事,您需要从单元格中手动删除它们,并确保替换或删除所有旧内容。

当您执行将图像下载并在一段时间后设置到单元格的方法时,单元格可能仍包含前一个单元格的图像。您应该清除 cellForRowAtIndexPath 方法开头的图像以删除旧图像/ subview 。

请看下面的例子:

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

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! GridViewCell


if indexPath.row == 0 {
// Show the title becouse it's the first cell.
cell.titleLabel.hidden = false

// Title needs to be set in storyboard
// Default image for camera roll needs to be set in storyboard

} else {
// All the other cells.
cell.titleLabel.hidden = true

// Clear old content
cell.imageView.image = nil

// I don't exactly know what all this code does behind the screen, but I assume it's a method that downloads the image and add it to the imageView when it's done.
let asset = assetsFetchResults[indexPath.item - 1] as! PHAsset
cell.representedAssetIdentifier = asset.localIdentifier

imageManager.requestImageForAsset(asset, targetSize: AssetGridThumbnailSize, contentMode: PHImageContentMode.AspectFill, options: nil) { (result: UIImage?, info: [NSObject : AnyObject]?) -> Void in
//print(result)
if cell.representedAssetIdentifier.isEqualToString(asset.localIdentifier) {
if let imageResult = result {
cell.imageView.image = imageResult
}
}
}
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

// Cell that needs to open the camera roll when tapped)
if indexPath.row == 0 {

// Do something that opens the camera roll

}

}

我没有测试这段代码,但它应该与此类似。

关于ios - UICollectionView CustomCell 重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33875133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com