相同单元格上的多个UICollectionView图层

时间:2015-04-06 06:03:20

标签: ios xcode swift

这可能是一个复杂的问题,所以请耐心等待。

完成从Parse加载对象并用它们填充CollectionViewCells后,即使我明确设置了self.collectionView?.bounces = false,它也会显示一个奇怪的弹跳动画。此外,标签清晰地相互映射,你可以看到一张图片在另一张图片下弹跳。

以下是我看到的错误:

https://youtu.be/q306SGM_tLEhttps://youtu.be/gK9HN877nRo

这是我处理细胞的方式:

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

    let nameLabelFont = UIFont(name: "Lato-Regular", size: 13)
    let priceLabelFont = UIFont(name: "Lato-Regular", size: 10)

    var priceLabel = UILabel(frame: CGRectMake(0, 0, 50, 40))
    var name = UILabel(frame: CGRectMake(0, 180, self.view.frame.size.width/2-5, 20))
    var unitLabel = UILabel(frame: CGRectMake(0, 200, self.view.frame.size.width/2-5, 20))
    var addToCartButton = UIButton(frame: CGRectMake(0, 220, 100, 30))

    if self.productImageArray.count != 0 {


        var image = self.productImageArray.objectAtIndex(indexPath.row) as UIImage
        var imageView = UIImageView(image: image)
        imageView.frame.size = CGSize(width: 187.5, height: 180)

        name.attributedText = NSAttributedString(
            string: self.productArray.objectAtIndex(indexPath.row).valueForKey("name") as NSString,
            attributes: [NSFontAttributeName : nameLabelFont!, NSForegroundColorAttributeName : UIColor.blackColor()])

        var price:Float = self.productArray.objectAtIndex(indexPath.row).valueForKey("price_tipsy") as Float
        priceLabel.backgroundColor = UIColor.redColor()
        priceLabel.attributedText = NSAttributedString(
            string: "$\(price)",
            attributes: [NSFontAttributeName : nameLabelFont!, NSForegroundColorAttributeName : UIColor.whiteColor()])

        unitLabel.attributedText = NSAttributedString(
            string: self.productArray.objectAtIndex(indexPath.row).valueForKey("unit") as NSString,
            attributes: [NSFontAttributeName : nameLabelFont!, NSForegroundColorAttributeName : UIColor.blackColor()])

        addToCartButton.setAttributedTitle(NSAttributedString(
            string: "ADD TO CART",
            attributes: [NSFontAttributeName : priceLabelFont!, NSForegroundColorAttributeName : UIColor.redColor()]),
            forState: UIControlState.Normal)
        addToCartButton.backgroundColor = UIColor.whiteColor()
        addToCartButton.layer.borderColor = UIColor.redColor().CGColor
        addToCartButton.layer.borderWidth = 1
        addToCartButton.layer.cornerRadius = 5



        cell.addSubview(imageView)
        cell.addSubview(name)
        cell.addSubview(priceLabel)
        cell.addSubview(unitLabel)
        cell.addSubview(addToCartButton)


    } else {
        cell.backgroundColor = UIColor.whiteColor()
    }
    return cell
}

修改

这是我的用户界面

enter image description here

及其调试视图层次结构

enter image description here

在刷新几次后,Debug View Hierarchy在同一个单元格上显示多个图层,层数与刷新次数相对应。

enter image description here

编辑2

这是我的自定义单元格类:

override init(frame: CGRect) {
    super.init(frame: frame)
}

func setup(frameWidth:CGFloat, image: UIImage, name: NSString, price:Float, unit:NSString) {

    let nameLabelFont = UIFont(name: "Lato-Regular", size: 13)
    let priceLabelFont = UIFont(name: "Lato-Regular", size: 10)

    var priceLabel = UILabel(frame: CGRectMake(0, 0, 50, 40))
    var nameLabel = UILabel()
    var unitLabel = UILabel()
    var addToCartButton = UIButton(frame: CGRectMake(0, 220, 100, 40))


    nameLabel = UILabel(frame: CGRectMake(0, 180, frameWidth/2-5, 20))
    unitLabel = UILabel(frame: CGRectMake(0, 200, frameWidth/2-5, 20))

    nameLabel.backgroundColor = UIColor.whiteColor()
    unitLabel.backgroundColor = UIColor.whiteColor()


    var imageView = UIImageView(image: image)
    imageView.frame.size = CGSize(width: frameWidth/2-5, height: 180)

    nameLabel.attributedText = NSAttributedString(
        string: name,
        attributes: [NSFontAttributeName : nameLabelFont!, NSForegroundColorAttributeName : UIColor.blackColor()])

    priceLabel.backgroundColor = UIColor.redColor()
    priceLabel.attributedText = NSAttributedString(
        string: "$\(price)",
        attributes: [NSFontAttributeName : nameLabelFont!, NSForegroundColorAttributeName : UIColor.whiteColor()])

    unitLabel.attributedText = NSAttributedString(
        string: unit,
        attributes: [NSFontAttributeName : nameLabelFont!, NSForegroundColorAttributeName : UIColor.blackColor()])

    addToCartButton.setAttributedTitle(NSAttributedString(
        string: "ADD TO CART",
        attributes: [NSFontAttributeName : priceLabelFont!, NSForegroundColorAttributeName : UIColor.redColor()]),
        forState: UIControlState.Normal)
    addToCartButton.backgroundColor = UIColor.whiteColor()
    addToCartButton.layer.borderColor = UIColor.redColor().CGColor
    addToCartButton.layer.borderWidth = 1
    addToCartButton.layer.cornerRadius = 5


    self.contentView.addSubview(imageView)
    self.contentView.addSubview(nameLabel)
    self.contentView.addSubview(priceLabel)
    self.contentView.addSubview(unitLabel)
    self.contentView.addSubview(addToCartButton)



}

就UICollectView类而言,在拉动RefreshController时会调用self.collectionView?.reloadData(),并且在这里:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("itemCell", forIndexPath: indexPath) as ShopCell
    cell.setup(self.view.frame.width, image: UIImage(), name: "AAA", price: 20.00, unit: "aaa")
    cell.contentView.backgroundColor = UIColor.orangeColor()
    return cell
}

0 个答案:

没有答案