集合单元在每个 reloadData() 调用 Swift 5 时删除项目
Posted
技术标签:
【中文标题】集合单元在每个 reloadData() 调用 Swift 5 时删除项目【英文标题】:Collection cell removes item on each reloadData() call Swift 5 【发布时间】:2020-03-27 04:49:22 【问题描述】:请看下图,这是我正确的初步看法。
但是在为 collectionView 调用 reloadData() 之后,它会删除“COMPLETED”状态标签,如果我再次重复该操作,它将删除“PROCESSING”状态标签。有谁知道它为什么会这样?
这是我的 cellForItemAt 代码。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DFCollectionViewCellIdentifierConstants.DF_CAPTURED_PANORAMAS_CELL, for: indexPath) as! DFCapturedPanoramasCollectionViewCell
cell.capturedPanoramasImages.layer.masksToBounds = true
cell.capturedPanoramasImages.layer.borderWidth = 1
cell.capturedPanoramasImages.layer.borderColor = UIColor().hexColor(DFColorConstants.INPUT_TEXT_COLOR).cgColor
cell.capturedPanoramasImages.layer.cornerRadius = 4
let url = URL(string:panoramaList[indexPath.item].panoramaThumbnailPath)
if url != nil && self.panoramaList[indexPath.item].panoramaStatus == DFPanoramaStatusConstants.DF_COMPLETED
self.panoramaStatusLabelFunc(cell: cell, index: indexPath.item, bgColor: UIColor().hexColor(DFColorConstants.PANORAMA_STATUS_COMPLETED_COLOR))
cell.capturedPanoramasImages.contentMode = .scaleAspectFill
if let data = try? Data(contentsOf: url!)
let image = UIImage(data: data)
cell.capturedPanoramasImages.image = image
cell.capturedPanoramasLabel.text = panoramaList[indexPath.item].name
else if url == nil && self.panoramaList[indexPath.item].panoramaStatus != DFStatusConstants.COMPLETED
self.panoramaStatusLabelFunc(cell: cell, index: indexPath.item, bgColor: UIColor().hexColor(DFColorConstants.PANORAMA_STATUS_UPLOADING_AND_PROCESSESING))
cell.capturedPanoramasImages.contentMode = .scaleAspectFit
cell.capturedPanoramasImages.image = self.capturedPanoramasImage[0]
cell.capturedPanoramasLabel.text = panoramaList[indexPath.item].name
else if self.panoramaList[indexPath.item].name == DFStatusConstants.ADD_PANORAMA
cell.panoramaStatusLabel.isHidden = true
cell.capturedPanoramasImages.contentMode = .center
cell.capturedPanoramasImages.image = self.capturedPanoramasImage[1]
cell.capturedPanoramasLabel.text = panoramaList[indexPath.item].name
return cell
还有panoramaStatusLabelFunc()函数。
func panoramaStatusLabelFunc(cell: DFCapturedPanoramasCollectionViewCell, index: Int, bgColor: UIColor)
cell.capturedPanoramasLabel.isHidden = false
cell.panoramaStatusLabel.layer.backgroundColor = bgColor.cgColor
cell.panoramaStatusLabel.textColor = UIColor().hexColor(DFColorConstants.TEXT_WHITE_COLOR)
cell.panoramaStatusLabel.layer.cornerRadius = 10
cell.panoramaStatusLabel.text = self.panoramaList[index].panoramaStatus
【问题讨论】:
【参考方案1】:发现问题,它处于“cell.panoramaStatusLabel.isHidden = true”的以下条件。出于某种原因,它正在将 isHidden = true 传递给下一个单元格(我不确定,但可能是因为 Asyn)
else if self.panoramaList[indexPath.item].name == DFStatusConstants.ADD_PANORAMA
cell.panoramaStatusLabel.isHidden = true
cell.capturedPanoramasImages.contentMode = .center
cell.capturedPanoramasImages.image = self.capturedPanoramasImage[1]
cell.capturedPanoramasLabel.text = panoramaList[indexPath.item].name
为了解决这个问题,我改变了全景状态标签函数如下:
func panoramaStatusLabelFunc(cell: DFCapturedPanoramasCollectionViewCell, index: Int, bgColor: UIColor, isAddHotspot: Bool)
cell.capturedPanoramasLabel.isHidden = false
cell.panoramaStatusLabel.layer.backgroundColor = bgColor.cgColor
cell.panoramaStatusLabel.textColor = UIColor().hexColor(DFColorConstants.TEXT_WHITE_COLOR)
cell.panoramaStatusLabel.layer.cornerRadius = 10
if(isAddHotspot)
cell.panoramaStatusLabel.text = ""
else
cell.panoramaStatusLabel.text = self.panoramaList[index].panoramaStatus
【讨论】:
以上是关于集合单元在每个 reloadData() 调用 Swift 5 时删除项目的主要内容,如果未能解决你的问题,请参考以下文章
移动时附加到 UICollectionView 中的单元格的弹出框被称为此集合视图的 ReloadData
UICollectionView reloadData() 不会更新集合视图中的单元格
UICollectionView reloadData 但查看闪烁