在 UICollectionView 中的第二个索引项处启动图像数组
Starting an Image array at the second index item in UICollectionView
我正在尝试让 "var product" 数组在第一个 UICollectionViewcell "newCell" 之后开始填充单元格。 newCell 将始终以相同的方式出现并始终存在。 productCell 可能具有不同的名称和标签,并且可能不会始终存在。图片数组 "products" 将继续增长,我目前可以看到第二个项目,但第一个图片被 newCell 集合视图单元格挡住了。
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,
谢谢!
因为products
数组只有两项。只需在数组的开头添加一个虚拟单元格,这样您就可以在索引零处修改集合而不会丢失其他项目。
var products: [itemCellContent] = {
var dummyCell = 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 [dummyCell, eggs, Toast]
}()
好的,你需要做两件事来实现这一目标。
首先,return 个单元格数比项目数多一个:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let _ = products {
return products!.count + 1
}
return 1
}
接下来,return根据上面的合适的单元格实例。
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
}
}
我正在尝试让 "var product" 数组在第一个 UICollectionViewcell "newCell" 之后开始填充单元格。 newCell 将始终以相同的方式出现并始终存在。 productCell 可能具有不同的名称和标签,并且可能不会始终存在。图片数组 "products" 将继续增长,我目前可以看到第二个项目,但第一个图片被 newCell 集合视图单元格挡住了。
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,
谢谢!
因为products
数组只有两项。只需在数组的开头添加一个虚拟单元格,这样您就可以在索引零处修改集合而不会丢失其他项目。
var products: [itemCellContent] = {
var dummyCell = 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 [dummyCell, eggs, Toast]
}()
好的,你需要做两件事来实现这一目标。 首先,return 个单元格数比项目数多一个:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let _ = products {
return products!.count + 1
}
return 1
}
接下来,return根据上面的合适的单元格实例。
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
}
}