0

我想在我的收集細胞子視圖添加UILabels。 但是當我滾動到其他細胞,回來全部UIlabels出現在兩個單元,有些是其他下寫..編程添加UILabels到收集細胞,迅速

我試圖與collectionView.cellForItemAtIndexPath(indexPath) as? CollectionViewCell,但是當我用它沒有用的UILabel顯示(我嘗試與突破點,它不會去if語句後)

我該如何解決?

謝謝!

class CollectionView: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 

     var collection = [PFObject]() 

     @IBOutlet weak var collectionView: UICollectionView! 

     let sidePadding:CGFloat = 10 

     var checkLocation: Bool = false 

     override func viewDidLoad() 
     { 
      super.viewDidLoad(); 
      self.collectionView!.delegate = self 
      self.collectionView!.dataSource = self 

     } 

     func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
      return 1 
     } 

     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
      return self.collection.count 
     } 

     func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 


      let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath) as! CollectionViewCell 

      cell.layer.borderColor = UIColor.lightGrayColor().CGColor 
      cell.layer.borderWidth = 0.5 
      cell.layer.cornerRadius = 3 

      let collectionViewWidth = self.collectionView.bounds.size.width - self.sidePadding * 2 
      cell.frame.size.width = collectionViewWidth 
      let collectionViewHeight = self.collectionView.bounds.size.height/4.5 
      cell.frame.size.height = collectionViewHeight 


      // Display the country name 
      if let value = self.collection[indexPath.row]["Name"] as? String { 
       cell.cellTitle.text = String(Int(indexPath.row)) + "." + value 

      } 

      let width = (cell.bounds.width - 94 - 4*3 - 60 - 20)/4 
      let cellheight = CGFloat(6) 
      let cellheight1 = CGFloat(6 + 12 + 2) 

      var array = [String]() 

      if let MWl = collection[indexPath.row]["MW"] as? Int 
      { 
       if MWl == 1 { 

        array.append("M") 

       } else if MWl == 2 { 

        array.append("M") 
        array.append("W") 

       } 


      } 

      if let JAl = collection[indexPath.row]["JA"] as? Int 
      { 
       if JAl == 1 { 

        array.append("Jy") 

       } else if JAl == 2 { 

        array.append("Jy") 
        array.append("Ac") 

       } 

      } 

      if let HK = collection[indexPath.row]["HK"] as? Int 
      { 
       if HK == 1 { 

        array.append("HB") 

       } else if HK == 2 { 

        array.append("HB") 
        array.append("Ks") 

       } 
      } 

      if let SSl = collection[indexPath.row]["SSl"] as? Int 
      { 
       if SSl == 1 { 

        array.append("AB") 

       } else if SSl == 2 { 

        array.append("AB") 
        array.append("CD") 

      } 

      //I tried here too 
      // if let myCell = collectionView.cellForItemAtIndexPath(indexPath) as? CollectionViewCell 

      if array.count <= 4 { 
       for i in 0..<array.count { 

        let taille = CGFloat(i) 
        var si = CGFloat((1+i)*3 + 94 + 30) 
        let label = UILabel() as UILabel 
        label.frame = CGRectMake((si + width*taille), cellheight, width, 12) 
        label.textColor = UIColor(red: 0/255, green: 125/255, blue: 255/255, alpha: 1.0) 
        label.backgroundColor = UIColor.clearColor() 
        label.layer.borderWidth = 0.7 
        label.layer.borderColor = UIColor.lightGrayColor().CGColor 
        label.font = UIFont(name: "Futura-Medium", size: 9) 
        label.textAlignment = NSTextAlignment.Center 
        label.numberOfLines = 1 
        label.text = array[i] 
        label.layer.masksToBounds = true 
        label.layer.cornerRadius = 3.5 
        if let myCell = collectionView.cellForItemAtIndexPath(indexPath) as? CollectionViewCell { 
         myCell.contentView.addSubview(label) 
        } 
       } 

      } else { 

       for i in 0...3 { 

        let taille = CGFloat(i) 
        var si = CGFloat((1+i)*3 + 94 + 30) 
        let label = UILabel() as UILabel 
        label.frame = CGRectMake((si + width*taille), cellheight, width, 12) 
        label.textColor = UIColor(red: 0/255, green: 125/255, blue: 255/255, alpha: 1.0) 
        label.backgroundColor = UIColor.clearColor() 
        label.layer.borderWidth = 0.7 
        label.layer.borderColor = UIColor.lightGrayColor().CGColor 
        label.font = UIFont(name: "Futura-Medium", size: 9) 
        label.textAlignment = NSTextAlignment.Center 
        label.numberOfLines = 1 
        label.text = array[i] 
        label.layer.masksToBounds = true 
        label.layer.cornerRadius = 3.5 
        if let myCell = collectionView.cellForItemAtIndexPath(indexPath) as? CollectionViewCell { 
         myCell.contentView.addSubview(label) 
        } 
       } 

       var j = 0 as Int 
       for i in 4..<array.count { 

        let taille = CGFloat(j) 
        let si = CGFloat((1+j)*3 + 94 + 30) 
        let label = UILabel() as UILabel 
        label.frame = CGRectMake((si + width*taille), cellheight1, width, 12) 
        label.textColor = UIColor(red: 0/255, green: 125/255, blue: 255/255, alpha: 1.0) 
        label.backgroundColor = UIColor.clearColor() 
        label.layer.borderWidth = 0.7 
        label.layer.borderColor = UIColor.lightGrayColor().CGColor 
        label.font = UIFont(name: "Futura-Medium", size: 9) 
        label.textAlignment = NSTextAlignment.Center 
        label.numberOfLines = 1 
        label.text = array[i] 
        label.layer.masksToBounds = true 
        label.layer.cornerRadius = 3.5 
        if let myCell = collectionView.cellForItemAtIndexPath(indexPath) as? CollectionViewCell { 
         myCell.contentView.addSubview(label) 
        } 
        j = j + 1 
       } 

      } 

      return cell 
     } 

    } 

編輯

我試圖以編程方式創建的UILabel。但通過這種方式,我使用了用StoryBoard設計的元素和以編程方式創建的元素。而實際上我不能因爲這一點:

 collectionView!.registerClass(RDCell.self, forCellWithReuseIdentifier: "Cell") 

我創建了小區的所有內容編程(在的CollectionView與故事板創建)和它完美的作品。

+0

因爲細胞是重新使用,添加標籤以便在單元格加載時標籤使用特定標籤去除標籤,您可以使用viewwithtag方法。 – NSLog

回答

0

該單元格正在重用,因此每次調用cellForItemAtIndexPath時都會添加新標籤。只需標記您的標籤一樣label.tag = 1,當你出列的單元格刪除所有子視圖與此標籤,像

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath) as! CollectionViewCell 
for view in cell.subviews { 
    if view.tag == 1 { 
     view.removeFromSuperview() 
    } 
} 

您將始終重新創建標籤,雖然,這是不好的設計。我建議在故事板上設計這些標籤,或者如果您堅持要在代碼中執行這些標籤,請在單元格中保留可選的對標籤的引用,以便每次調用cellForItemAtIndexPath時不重新創建它們。

+0

謝謝@FruitAddict,我試過了,但是當我移動一點點單元格時,它將刪除所有標籤。我想以編程的方式來做,而不是使用UIStackView,因爲我的目標是iOS8。我在我的CollectionCell(var label:UILabel?...)中創建了8個UILabels,然後給它們分配一個文本值和一個位置讀數給array.count。但沒有任何顯示。你知道發生了什麼嗎?謝謝! –

0

不推薦,但你可以做的是, 添加標籤每個標籤像1000或東西,發現小區的子觀點如有子視圖標籤與標籤標籤匹配刪除視圖

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
{ 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath) as! CollectionViewCell 

    for let labelIs in cell.subviews 
     { 
      if labelIs.tag == 1000 
      { 
       labelIs.removeFromSuperview() 
      } 
     } 
     //some whare 
     var label = UILabel() 
     label.tag = 1000 
     return cell 
} 
+0

謝謝@NSLog,我試過了,但是當我移動一點點單元格時,它將刪除所有標籤。我在我的CollectionCell(var label:UILabel?...)中創建了8個UILabels,然後給它們分配一個文本值和一個位置讀數給array.count。但沒有任何顯示。你知道發生了什麼嗎?謝謝! –

+0

標籤永遠不會出現?,這種方式它應該工作。 – NSLog

+0

沒有出現..我不明白..我添加了我的腳本編輯。你看到錯誤在哪裏? –