2017-08-02 87 views
0

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

这是一个约束问题或者是我的错误代码?你可以看一下右边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 

    } 

回答

2

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

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

集合视图使用可重复的细胞,使标签的文字颜色可能仍然是白色的被以前使用的。

+0

为什么如果在故事板中默认设置为黑色? – GaTz

+0

因为它们是“可重复使用的单元格”。当您调用“collectionView.dequeueReusableCell”时,您正在创建一个先前创建的单元格并在其上设置新数据。你不是从故事板创建它。该单元格将仍然具有所有旧数据。 (随着标签设置,如文字颜色) – Jboullianne

+0

我认为你完全正确@Jboullianne +1 –