收集单元格在每次调用 reloadData() 时删除项目 Swift 5
Collection cell removes item on each reloadData() call Swift 5
请看下图,这是我正确的初步看法。
但是在为 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
}
发现问题,它处于以下状态 "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
}
为了解决这个问题,我更改了 panoramaStatusLabelFunc 函数,如下所示:
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
}
}
请看下图,这是我正确的初步看法。
但是在为 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
}
发现问题,它处于以下状态 "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
}
为了解决这个问题,我更改了 panoramaStatusLabelFunc 函数,如下所示:
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
}
}