2017-08-02 92 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 –