2017-03-10 64 views
0

好的,我對這裏的集合視圖缺乏理解,以及它們如何不斷實例化單元。我將MsStickerViews與該視圖的本地描述(名稱)一起添加到我的集合視圖中。理想情況下,我只想這樣做一次(不是每次用戶滾動)。根據我的理解,標準收藏查看功能Swift集合視圖不會添加超過6個項目?

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

每當用戶滾動並且會;只要不斷添加你一次又一次地放入這個函數。爲了解決這個問題,我創建一個單獨的類StickerCollectionViewCell在那裏我有這些初始化函數:

public var animalName = String() 
    var logoLabel = UILabel() 
    public var hasSticker = Bool(false) 
public func initSticker(stickerView: MSStickerView) 
    { 
      print("PUTTING: ",stickerView.sticker?.localizedDescription) 
      self.addSubview(stickerView) 
      stickerView.startAnimating() 
    } 

    public func initLabels() 
    { 
     //print("called", animalName) 
     logoLabel.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: 100) 
     logoLabel.textColor = UIColor.black 
     logoLabel.textAlignment = .center 
     //logoLabel.text = stickerPack[indexPath.item].sticker?.localizedDescription.uppercased() 
     logoLabel.text = animalName.uppercased() 
     logoLabel.font = UIFont(name: "Futura-Bold", size: screenSize.height * (9.8/screenSize.height)) 
     logoLabel.center = CGPoint(x: self.bounds.width * 0.5, y: self.bounds.height * 0.96) 
     self.addSubview(logoLabel) 
    } 

我再加入我的細胞是這樣的:

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

     // get a reference to our storyboard cell 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "stickerCell", for: indexPath as IndexPath) as! StickerCollectionViewCell 

      cell.backgroundColor = UIColor.clear// make cell more visible in our example project 

     if(cell.hasSticker == false && animalNames.contains((stickerPack[indexPath.item].sticker?.localizedDescription)!) == false) 
     { 
      cell.initSticker(stickerView: stickerPack[indexPath.item]) 
      cell.animalName = (stickerPack[indexPath.item].sticker?.localizedDescription)! 
      cell.initLabels() 

      animalNames.append(cell.animalName) 
      cell.hasSticker = true; 
     } 
     print("Animal names", animalNames) 

     return cell 
    } 

的原因if語句我加入我的從一個名稱數組貼圖視圖,直到今天我只有6個名字在數組中。現在用7,它開始將數組中的最後一項添加到另一個單元格的頂部,即使我計算了它,它仍然會生成7個單元格。

布爾和存儲已用完的名稱數組已幫助他們現在不會重疊,但最後一個項目永遠不會被添加。它不會放入用過的名稱數組中。當我滾動時,單元格中的貼紙會左右移動,這意味着當我滾動到底部時,頂部的貼紙將位於底部。

我檢查,我創建單元格的右邊數:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     print("COUNT",self.animalsAnim.count) 
     return self.animalsAnim.count 
    } 

我不知道是什麼原因造成這一點。我怎樣才能阻止細胞持續的清新?爲什麼我的字符串數組中的最後一項不會被添加?哪裏不對?

+0

你不想要'if'語句。隨着collectionview滾動,每當需要一個單元時,都會調用celForRowAt。您可以使先前使用的單元對象出隊。你的代碼需要根據indexpath – Paulw11

回答

0

啊,沒關係。這工作:

override func prepareForReuse() { 
     super.prepareForReuse() 

     for view in self.subviews{ 
      view.removeFromSuperview() 
     } 
    } 
+0

來設置所有適當的視圖。這很有效,但效率不高。由於您已經擁有自定義單元類,因此在故事板中創建一個單元格模板,將其設置爲您的自定義類的類,並在其中添加所需的字段。然後用插座將它們連接到自定義類。現在,您的單元格將在創建後立即擁有所需的字段,並且每次滾動時都不會添加和刪除單元格中的字段。 –

1

使用不同/更好的方法。

在故事板中創建單元格模板。將模板中單元格的類設置爲您的自定義類。將所需的字段添加到單元格,並使用插口將它們連接到單元格。然後擺脫所有將自定義字段添加到單元格的代碼。只需在cellForItemAt方法中將值添加到已添加的單元格字段中即可。