uicollectionviewios13swift5xcode11uicollectionreusableview

Collection cell removes item on each reloadData() call Swift 5


Please see below image, that is my correct and initial view. enter image description here

But after reloadData() is called for collectionView then it is removing the "COMPLETED" status label, and if I again repeat that action it will remove the "PROCESSING" status label. Does anyone know why it is behaving like this?

enter image description here

This is my cellForItemAt code.

    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
    }

And panoramaStatusLabelFunc() function.

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
    }

Solution

  • Found the issue, it is in below condition with "cell.panoramaStatusLabel.isHidden = true". For some reason it is passing isHidden = true to next cell(I an not sure, but may be beacuse fo 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
    }
    

    To fix I have changed panoramaStatusLabelFunc function as below:

        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
            }
        }