0

我正在尝试在Collection Cell的subView中添加 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 是使用情节提要创建的)并且它运行良好。

4

2 回答 2

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
}
于 2016-03-16T21:26:10.757 回答
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调用时都重新创建它们。

于 2016-03-16T21:23:18.053 回答