ios - 如何在 Collection View 中将所有项目显示为第一个索引

标签 ios swift uicollectionview

我有一个 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/

相关文章:

ios - 如何在动态类型大小更改时更新 WKWebView?

ios - 如何在 Cordova 中为 ios 启动屏幕使用单个图像

ios - 如何使用 Swift 3 将单例与 Alamofire 一起使用?

ios - xcode 7 和 cloudkit 的非常奇怪的问题

ios - 使用自动布局更改 UIButtons 的列布局

ios - 将 UITextField 保存到 UICollectionViewCell 中

ios - OSX 上的 AVAudioSession 替代方案以获取音频驱动程序采样率

iphone - 从iPhone应用程序打开PDF

swift - 将 swift 结构的地址转换为特定类型的 C 指针

ios - 如何在 Swift 中将数组存储到 PFFile 中?