【问题标题】:Collection view cell content is layering集合视图单元格内容是分层的
【发布时间】:2014-12-28 05:40:39
【问题描述】:

当我滚动时,我的按钮标签会堆叠/重新生成。因此,在离开视图并重新进入后,第一个标签可能会在其顶部显示带有 Elabel 的 Alabel。我只是想创建一行可滚动的按钮(或标签)。集合视图和单元格是通过 Storyboard 创建的。代码在 CV 中生成正确数量的单元格,但标签在滚动时会分层(水平)。

let buttonLabels = ["Alabel", "Blabel", "Clabel", "Dlabel", "Elabel", "Flabel", "Glabel", "Hlabel", "Ilabel", "Jlabel", "Klabel", "Llabel", "Mlabel"]

@IBOutlet weak var btnCollVw: UICollectionView!

//loadColFlowLayout() is called from ViewDidLoad()
func loadColFlowLayout() { 
        let btnLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        btnLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        btnLayout.sectionInset = UIEdgeInsets(top: 0, left: 1, bottom: 0, right: 1)
        btnLayout.itemSize = CGSize(width: 63, height: 30)
        btnCollVw.collectionViewLayout = btnLayout
        btnCollVw!.backgroundColor = UIColor.whiteColor()
    }

func numberOfSections() -> Int {
       return 1
       }

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

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        var cell: UICollectionViewCell = self.iconCollVw.dequeueReusableCellWithReuseIdentifier("swIconsCell", forIndexPath: indexPath) as UICollectionViewCell

        var makeButton = UIButton(frame: CGRectMake(0, 0, 63, 29))
        makeButton.setTitle(buttonLabels[indexPath.item], forState: .Normal)
        makeButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
        cell.addSubview(makeButton)
        // or using cell.contentView.addSubview(makeButton)

    return cell

    }
}

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell


    【解决方案1】:

    单元格重用的问题,每次从集合视图中取出单元格时,它已经有按钮,当我检查单元格上带有已知标签的按钮时,看看我的改进版本:

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        var cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("swIconsCell", forIndexPath: indexPath) as UICollectionViewCell
        if cell.viewWithTag(1234) == nil {
            var makeButton = UIButton(frame: CGRectMake(0, 0, 63, 29))
            makeButton.setTitle(buttonLabels[indexPath.item], forState: .Normal)
            makeButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
            makeButton.tag = 1234;
            cell.addSubview(makeButton)
            // or using cell.contentView.addSubview(makeButton)
        }
        return cell
    }
    

    你可以用另一种方式做同样的事情,例如创建UICollectionViewCell的子类

    【讨论】:

    • 非常感谢!我尝试了一个 if 条件,但没有使用标签。由于代码仅在屏幕按钮上生成,因此仍然存在一些问题。在 iPhone6 sim 上,按钮在 6 后以纵向视图重新开始,在 iPad 模拟器上,它会生成整个按钮列表。重叠问题消失了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多