2017-07-13 206 views
0

我正在使用一個collectionview和第一個數據加載工作正常。當數據第二次加載標籤重疊時會發生問題,因爲舊標籤似乎仍然存在。 以下是我的收藏查看代碼,在此先感謝。UICollectionView重疊標籤

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
       let customCell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCellIdentifier", for: indexPath) 
       customCell.layer.borderColor = UIColor.black.cgColor 
       customCell.layer.borderWidth = 1 


    // changed these lines here 
       if cellsLabels[indexPath.row] != nil { 
        customCell.willRemoveSubview(cellsLabels[indexPath.row]!) 
       } 
    //to these lines here and the problem was solved 
      let maybe = customCell.subviews 

      for i in 0 ..< maybe.count { 
       maybe[i].removeFromSuperview() 
      } 
       let c 

ommentLabel = UILabel() 
      commentLabel.text = commentsArray[indexPath.row] 
      commentLabel.frame = CGRect(x: 0, y: 50, width: 200, height: 30) 
      customCell.addSubview(commentLabel) 

      self.cellsLabels[indexPath.row] = commentLabel 

      if indexPath.row == commentLoadTracker*10 - 1 { 
       print("working doomfist") 
      } 

      return customCell 
     } 

     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

      return commentsArray.count 
     } 

     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
      var cellSize = CGSize() 
      cellSize.width = self.commentView.frame.width 
      cellSize.height = 100 
      return cellSize 
     } 
+0

你並不需要用你的結果更新問題。這可能會帶來問題的明確性。此外,你可以像這樣循環訪問子視圖:'在customCell.subviews中查看',那麼你不必創建一個新的數組。 –

回答

1

customCell.willRemoveSubview

要調用willRemoveSubiew代替removeFromSuperview

if cellsLabels[indexPath.row] != nil { 
    cellsLabels[indexPath.row]!.removeFromSuperview() 
} 

無需調用willRemoveSubview,呼籲的UIKit爲你,它只存在這樣它可以是

在子視圖從視圖中刪除之前,由子類重寫以執行其他操作。

+0

還有你!問題解決了,還有另一個小問題,但基本上把這個抓起來感謝了! – Blue

+0

@AaronMann這沒問題!贊成票和標記它接受,將不勝感激! –