我有一个 UICollectionView
,我在其中获取一些类别名称并显示该名称,当用户按下 UICollectionView
中的任何单元格时,该特定数据将显示在不同的 UICollectionView
中(第二个 UICollectionView
)。
所以我需要的是,例如在我的 UICollectionView
中是这样的:
"Gems gaster royel mems"
我需要展示:
"All items Gems gaster royel mems"
如何在我的 Collection View 的第一个索引中动态添加所有项目
,这样如果用户按下该 Collection View 单元格 - 所有项目 - 我需要显示我的数据库中可用的所有产品。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionview1.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! smallDetail_cell
cell.productName.text = BTdata2[(indexPath as NSIndexPath).row].BTNames2
return cell
}
最佳答案
var selectedindex : NSInteger = 0
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
if(collectionView == collectionview1)
{
return BTdata2.count + 1
}
else
{
return Productdata.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionview1.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! smallDetail_cell
//cell.productName.text = BTdata2[(indexPath as NSIndexPath).row].BTNames2
if selectedindex == indexPath.row{
cell.productName.textColor = UIColor.red
}else{
cell.productName.textColor = UIColor.black
}
if indexPath.row == 0 {
cell.productName.text = "All Items"
}else{
cell.productName.text = BTdata2[(indexPath as NSIndexPath).row - 1].BTNames2!
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
if collectionView == collectionview1 {
if indexPath.row == 0 {
print("all item show here click")
}else{
print("your button click here like ",BTdata2[(indexPath as NSIndexPath).row - 1].BTNames2!)
// write code for display all selected category item..
}
selectedindex = indexPath.row
collectionview1.reloadData()
}
}
输出:
点击此处显示所有项目
你的按钮像智能手机一样点击这里
你的按钮像笔记本电脑一样点击这里
关于ios - 如何在 Collection View 中将所有项目显示为第一个索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40188543/