标签从CollectionViewCell中消失

时间:2017-08-02 14:35:25

标签: ios swift uicollectionview uicollectionviewcell

我正在使用collectionView,在第一个indexPath行中,我有一个来自第一个从Firebase检索到的viewController的数据摘要,从第二个开始我已经隐藏了所有标签并显示了其他人但是当我回到第一个视图并返回时进入collectionView标签消失

这是约束的问题还是我的错误代码?你可以看右侧3个标签消失

在:

First

后:

Second

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cella = collectionView.dequeueReusableCell(
            withReuseIdentifier: "cella", for: indexPath) as! ChatViewCell
        cella.LabelEur.text = ""
        if (indexPath.row == 0){
            collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
            cella.layer.backgroundColor = UIColor("#FFA000").cgColor

            cella.Label1.textColor = .white
            cella.LabelR.textColor = .white
            cella.Date.textColor = .white
            cella.Time.textColor = .white
            cella.City.textColor = .white

            let user = Chat[indexPath.row]
            cella.Label1.text = user.sender
            cella.LabelEur.isHidden = true
            cella.euro.isHidden = true
            cella.Tipo.isHidden = false
            cella.Quantita.isHidden = false
            cella.Misure.isHidden = false
            cella.TIpo2.isHidden = false
            cella.Label1.isHidden = false
            cella.labeldata.isHidden = true
            cella.labelora.isHidden = true
            cella.Date.text = user.date
            cella.Time.text = user.time

            if user.caricoc == "1" {
                cella.CaricoC.isHidden = false
            } else { cella.CaricoC.isHidden = true }
            if user.runflat == "1" {
                cella.RunFlat.isHidden = false
            } else { cella.RunFlat.isHidden = true }
            if user.rinforzato == "1" {
                cella.Rinforzato.isHidden = false
            } else { cella.Rinforzato.isHidden = true }
            cella.montaggio.isHidden = true
            cella.oraMontaggio.isHidden = true
            cella.Distanza.isHidden = true
            cella.categoria.isHidden = false
            cella.categoria.text = user.cat
            cella.TIpo2.text = user.tipo
            cella.Misure.text = user.model
            cella.Tipo.text = user.marca
            cella.Quantita.text = user.numero
            cella.LabelR.text = "Ha inserito una richiesta"
            //key2 = user.key
            misure1 = user.model
            caricC = user.caricoc
            rnf = user.runflat
            rinf = user.rinforzato
            tipo = user.tipo
            marca = user.marca
            quantita = user.numero
            cat = user.cat
            //last = user.lastname
            lat = user.lat
            lng = user.lng
        }else{

        }
        if (indexPath.row >= 1){
            let user = Chat[indexPath.row]
            cella.Label1.text = ""
            cella.Date.text = user.date
            cella.Time.text = user.time
            cella.RunFlat.isHidden = true
            cella.Rinforzato.isHidden = true
            cella.CaricoC.isHidden = true
            cella.LabelEur.isHidden = false
            cella.categoria.isHidden = true
            cella.euro.isHidden = false
            cella.TIpo2.isHidden = true
            cella.Tipo.isHidden = true
            cella.Quantita.isHidden = true
            cella.labelora.isHidden = false
            cella.labeldata.isHidden = false
            cella.montaggio.isHidden = false
            cella.oraMontaggio.isHidden = false
            cella.Distanza.isHidden = false
            cella.oraMontaggio.text = user.oraApp
            cella.montaggio.text = user.dataApp
            cella.Misure.isHidden = true

            let prezzo: Float = (Float(user.text)?.multiplied(by: 1.10))!
            let nenno: Float = 3.234
            let prezzo2 = String(describing: NSDecimalNumber(string: String(format:"%.0f", prezzo)))
            cella.LabelEur.text = prezzo2
            cella.LabelR.isHidden = true
            cella.layer.backgroundColor = UIColor("#FFFFFF").cgColor
            let posInt = Int(user.positiondef)
            if (posInt! <= 5) {
                cella.Distanza.text = "A meno di 5km"
            } else if (posInt! <= 10) {
                cella.Distanza.text = "A meno di 10km"
            } else if (posInt! <= 15) {
                cella.Distanza.text = "A meno di 15 km"
            }
            /*
             if (user.positiondef != nil) {
             if (user.positiondef.hasPrefix("0")) {
             cella.Distanza.text = "Nelle vicinanze"
             } else {
             cella.Distanza.text = user.positiondef+"KM"
             }
             }*/
            cella.City?.text = user.city
            self.Chat = self.Chat.sorted{ $0.text.compare($1.text, options: .numeric) == ComparisonResult.orderedAscending }

        }

        return cella

    }

1 个答案:

答案 0 :(得分:2)

我认为限制是正确的。它们不应该在viewController转换之间进行更改。问题可能在于标签的“textColor”属性。在第一个单元格中,您将颜色设置为白色。在第二个他们应该是黑色的。我认为它们不仅仅因为它们与背景颜色相同而不能显示。尝试在第二个“if”语句中将标签的textColor设置为黑色。

if indexPath.row >= 1 {
    //set textColor of labels here.
}

集合视图使用可重复使用的单元格,因此标签的textColor可能仍然是以前使用过的白色。