集合视图 - 泡泡边框和填充颜色更改为不同的颜色

问题描述 投票:0回答:1

我正在使用集合视图,我们已经向单元格添加了气泡。此外,气泡具有不同的填充颜色和边框颜色。

请查找附件了解详情。

enter image description here

但是当我们滚动集合视图时,时间泡泡颜色有时会改变为不同并再次恢复到正确的颜色。

这是我的代码:

          func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {   

// INIT CELLS INSIDE COLLECTION VIEW

if(collectionView == customContentCollectionView){

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: contentCellId, for: indexPath) as! MyContentCell

    setupContentCellComponents(cell: cell)

    // Configure the cell
    cell.horizontalLine.backgroundColor = Color.lightBlue

    var labelColour = UIColor()
    // If we found record
    if(self.bubbleArray[indexPath.section][indexPath.item].interactions != ""){         //  GENERATING CUSTOM BUBBLE COLOR AS PER INTERACTIONS
        let (bubbleBorder, bubbleFill, labelColor, inspectorNav) = getBubbleColor(controlType: controlParam, count: Int(self.bubbleArray[indexPath.section][indexPath.item].interactions)!, selected: false)

        cell.shapeLayer.strokeColor = bubbleBorder.cgColor
        cell.shapeLayer.fillColor = bubbleFill.cgColor
        cell.gradient.colors = [bubbleFill.cgColor, bubbleFill.cgColor]
        labelColour = labelColor
    }

    cell.labelCount.font =  UIFont(name: cellFontName, size: cellFontSize)
    cell.labelCount.text = self.bubbleArray[indexPath.section][indexPath.item].interactions
    if (self.bubbleArray[indexPath.section][indexPath.item].umid != ""){
        cell.tag = Int(self.bubbleArray[indexPath.section][indexPath.item].umid)!
        cell.labelCount.tag = Int(self.bubbleArray[indexPath.section][indexPath.item].umid)!
        cell.labelCount.textColor = labelColour
    }
    else  (self.bubbleArray[indexPath.section][indexPath.item].umid == ""){           //  REMOVING BUBBLE IF NO CONTENT
        cell.shapeLayer.removeFromSuperlayer()//remove from superview
    }

}

这是滚动逻辑:

 func scrollViewDidScroll(_ scrollView: UIScrollView) {
// selectedSection is the value where you have tapped
if(selectedSection != -1){
        for i in 0...numberOfItemsInSection{                                                      //  CODE TO MAINTAIN ROW HIGHLIGHT POST SCROLL
            let newIndices = IndexPath(row: i, section: selectedSection)
            for visibleIndices in customContentCollectionView.indexPathsForVisibleItems{
                if(newIndices == visibleIndices){
                    print("NEW INDICES: \(newIndices)")
                    if(newIndices.section == selectedSection){
                        selectedRowCells.append(customContentCollectionView.cellForItem(at: newIndices)!)
                    }
    // horizontal line for selected bubble
    let singleCell : MyContentCell = customContentCollectionView.cellForItem(at: newIndices)! as! MyContentCell
                        singleCell.horizontalLine.backgroundColor = UIColor.white

                        if(previouslySelectedIndex != nil && visibleIndices == previouslySelectedIndex){
                            changeBubbleColor(index: previouslySelectedIndex, selected: true)
                            break
                        }
    }
   }
ios swift
1个回答
0
投票

尝试这种方式,如果单元格满足条件,则设置属性,但如果条件不满足,那么单元格将采用条件中设置的先前属性,因此您还需要在else条件中设置属性

所以尝试通过以下代码中的注释设置属性

我希望你能得到你的解决方案

func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //  INIT CELLS INSIDE COLLECTION VIEW

        if(collectionView == customContentCollectionView){

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: contentCellId, for: indexPath) as! MyContentCell

            setupContentCellComponents(cell: cell)

            // Configure the cell
            cell.horizontalLine.backgroundColor = Color.lightBlue

            var labelColour = UIColor()
            // If we found record
            if(self.bubbleArray[indexPath.section][indexPath.item].interactions != "")
            {         //  GENERATING CUSTOM BUBBLE COLOR AS PER INTERACTIONS
                let (bubbleBorder, bubbleFill, labelColor, inspectorNav) = getBubbleColor(controlType: controlParam, count: Int(self.bubbleArray[indexPath.section][indexPath.item].interactions)!, selected: false)

                cell.shapeLayer.strokeColor = bubbleBorder.cgColor
                cell.shapeLayer.fillColor = bubbleFill.cgColor
                cell.gradient.colors = [bubbleFill.cgColor, bubbleFill.cgColor]
                labelColour = labelColor
            }
            else
            {
                //set default cell.shapeLayer.strokeColor
                //set default cell.shapeLayer.fillColor
                //set default cell.gradient.colors
                //set default labelColour
            }

            cell.labelCount.font =  UIFont(name: cellFontName, size: cellFontSize)
            cell.labelCount.text = self.bubbleArray[indexPath.section][indexPath.item].interactions
            if (self.bubbleArray[indexPath.section][indexPath.item].umid != "")
            {
                cell.tag = Int(self.bubbleArray[indexPath.section][indexPath.item].umid)!
                cell.labelCount.tag = Int(self.bubbleArray[indexPath.section][indexPath.item].umid)!
                cell.labelCount.textColor = labelColour
            }
            else
            {
                //Set default cell.tag
                //Set default cell.labelCount.tag
                //Set default cell.labelCount.textColor

            }

        }
© www.soinside.com 2019 - 2024. All rights reserved.