作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 UIView
中有一个 UICollectionView
。我想做的是当用户按下按钮时,其他项目(图像数组)出现在相同的 UICollectionView
上。
假设我有两个项目数组:
let items = [UIImage(named: "moses-vega-436582-unsplash"),
UIImage(named: "april6"), UIImage(named: "april4"), UIImage(named:
"april5")]
let items2 = [UIImage(named: "test01"), UIImage(named: "test02")]
现在,当用户按下按钮时,我想用 items2 中的图像更新我的 collectionView。我正在使用集合的基本代码(我很容易检测要显示哪些标签。因为我有一个名为“Testas”的变量,如果它是 0,那么我知道它是默认的 collectionView,否则它是......:
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
if Testas == 0 {
cell.image.image = items[indexPath.item]
if indexPath.item == 0 {
cell.label.text = "One"
}
if indexPath.item == 1 {
cell.label.text = "Two"
}
if indexPath.item == 2 {
cell.label.text = "Collection 3"
}
if indexPath.item == 3 {
cell.label.text = "Rainy Days"
}
} else {
cell.image.image = items2[indexPath.item]
if indexPath.item == 0 {
cell.label.text = "White"
}
if indexPath.item == 1 {
cell.label.text = "Blue"
}
}
return cell
}
总而言之,我想问的是,当用户按下按钮时,我需要编写什么来将 items2 传递到 collectionView 中,以及如何使这个 collectionView 出现? (因为它不是一个函数,也不是我可以轻易调用的东西,我猜)。请记住,我有一个计算项目的功能。所以这是最大的问题。当用户按下按钮然后显示图像时,我需要一个函数来计算我的项目 2。太感谢了。也许用这种方式甚至不可能做出我想要的东西。我不知道。
最佳答案
您可以通过创建一个枚举轻松完成此操作,然后在按下按钮时简单地切换它的值,然后是 collectionView.reloadData()
这是你的枚举:
enum ItemType : Int {
case items = 0,
items2 = 1
}
声明如下:
var itemType : ItemType = .items
您的 collectionView 函数看起来像这样:
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return itemType == .items ? items.count : items2.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
switch itemType {
case .items:
// code for items array
default:
// code for items2 array
}
return cell
}
你的按钮按下:
@IBAction func onButtonPressed(_ sender: Any) {
itemType = .items2
collectionView.reloadData()
}
如果您有超过 2 个数组,则需要将您的 collectionView 函数更新为如下所示:
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
switch itemType {
case .items:
return items.count
case .items2:
return items2.count
case .items3:
return items3.count
default:
return items4.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
switch itemType {
case .items:
// code for items array
case .items2:
// code for items2 array
case .items3:
// code for items3 array
default:
// code for items4 array
}
return cell
}
关于ios - 如何在一个 collectionView 中使用多个项目数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56060172/
我是一名优秀的程序员,十分优秀!