mack
mack

Reputation: 611

How to show All items as first index in collection view

I have one UICollectionView , in which I am fetching some category name and i am display that name and when user press any cell in UICollectionView, that particular data will show in down of different UICollectionView (second UICollectionView).

So what I need is, for example in my UICollectionView is look like this means :

"Gems gaster royel mems"

I need to show :

 "All items Gems gaster royel mems"

How to add dynamically All items in my first index of my collection view, so that if user press that collection view cell - All items-I need to show all product that available in my db.

 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

        }

Upvotes: 0

Views: 1499

Answers (2)

Himanshu Moradiya
Himanshu Moradiya

Reputation: 4815

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()
        }

    }

Output :

all item show here click

your button click here like Smartphones

your button click here like Laptops

Upvotes: 1

Zaid Pathan
Zaid Pathan

Reputation: 16820

Try this,

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        let cell = collectionview1.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! smallDetail_cell

        if indexPath.row == 0{
            cell.productName.text = "All items"
        }else{
            cell.productName.text = BTdata2[((indexPath as NSIndexPath).row) - 1].BTNames2
        }

        return cell
    }

Update your collectionView(_ numberOfItemsInSection:) with following,

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return BTdata2.count + 1
}

Upvotes: 0

Related Questions