swift - 在 UICollectionView 中的第二个索引项处启动图像数组

标签 swift uicollectionview uicollectionviewcell indexpath

我试图让“var Product”数组开始填充第一个 UICollectionViewcell“newCell”之后的单元格。 newCell 将始终以相同的方式出现并且始终存在。 ProductCell 可能有不同的名称和标签,并且可能并不总是存在。图像“产品”数组将继续增长,我当前可以看到第二个项目,但第一个图像被 newCell Collection View 单元格挡住。

var products: [itemCellContent] = {



    var eggs = itemCellContent()
    eggs.itemName = "Eggs"
    eggs.itemImageName = "eggs"
    eggs.Quantity = "1"

    var Toast = itemCellContent()
    Toast.itemName = "Bread"
    Toast.itemImageName = "bread"
    Toast.Quantity = "5"


    return [eggs, Toast]
}()

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

    if indexPath.item == 0
    {
        let newCell = collectionView.dequeueReusableCell(withReuseIdentifier: addCellID, for: indexPath) as! addProductCell


        print("Rounds corners")

        return newCell
    } else {
        let productCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! productCellLayout
        productCell.backgroundColor = UIColor.rgb(red: 252, green: 252, blue: 252) //off white blue color
        productCell.layer.cornerRadius = 5
        print("Rounds corners")

        productCell.gItem = products[indexPath.item]

        //collectionview.insertIems(at: indexPaths)

        //Shadow
        productCell.layer.shadowColor = UIColor.gray.cgColor
        productCell.layer.shadowOffset = CGSize(width: 0, height: 3.0)
        productCell.layer.shadowOpacity = 0.7
        productCell.layer.shadowRadius = 2.0
        productCell.layer.masksToBounds = false
        productCell.layer.shadowPath = UIBezierPath(roundedRect: productCell.bounds, cornerRadius: productCell.contentView.layer.cornerRadius).cgPath;
        return productCell
    }
}

如果有人可以帮助我获取“var Product”数组,以将第二个单元格识别为可填充的第一个单元格,请告诉我我能做什么。

This is what I see when I run the code This is what I want to see,

谢谢!

最佳答案

好的,您需要做两件事才能实现这一目标。 首先,返回单元格数量比项目数量多一:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if let _ = products {
        return products!.count + 1
    }
    return 1
}

接下来,根据上面返回适当的单元格实例。

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

    if indexPath.item == 0
    {
        let newCell = collectionView.dequeueReusableCell(withReuseIdentifier: addCellID, for: indexPath) as! addProductCell


        print("Rounds corners")

        return newCell
    } else {
        let productCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! productCellLayout
        productCell.backgroundColor = UIColor.rgb(red: 252, green: 252, blue: 252) //off white blue color
        productCell.layer.cornerRadius = 5
        print("Rounds corners")

        productCell.gItem = products[indexPath.item - 1] // Here's a difference than before

        //collectionview.insertIems(at: indexPaths)

        //Shadow
        productCell.layer.shadowColor = UIColor.gray.cgColor
        productCell.layer.shadowOffset = CGSize(width: 0, height: 3.0)
        productCell.layer.shadowOpacity = 0.7
        productCell.layer.shadowRadius = 2.0
        productCell.layer.masksToBounds = false
        productCell.layer.shadowPath = UIBezierPath(roundedRect: productCell.bounds, cornerRadius: productCell.contentView.layer.cornerRadius).cgPath;
        return productCell
    }
}

关于swift - 在 UICollectionView 中的第二个索引项处启动图像数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51996675/

相关文章:

ios - 动画 Collection View 高度变化(Swift)

ios - 淡出 UICollectionView 中的项目

swift - 为什么向 UICollectionViewCell 中的按钮添加操作效果不佳?

ios - Collection View 单元格的内容大小不正确

swift - 使用自定义类设置 UICollectionView 数据源和委托(delegate)时出现错误消息

swift - 如何从 Swift 中的另一个类访问枚举?

ios - 如何在签证结帐中使用文本字段数据作为货币金额

arrays - 如何从数组中删除与另一个数组中的元素匹配的元素

ios - 当前操作后将 Collection View 的委托(delegate)设置为 self

ios - 如何使用 Swift 在 iOS 8 中正确添加 subview Controller