iosswiftswift3uicollectionview

Multiple Collection Views with same XIB Cell and button


I have a View Controller with multiple Collection Views.

Each collection view is using the same custom cell with xib. In this xib I have a button.

The collectionViews names are

1) manPerfumeCV
2) womanPerfumeCV
3) kidsPerfumeCV

Inside cellForItemAt I have cell.productCart.tag = indexPath.row

    let cell:iPhoneCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "iPhoneCell", for: indexPath) as! iPhoneCollectionViewCell
    if collectionView == self.womanPerfumeCV {
        
        let prods = womanPerfumeData[indexPath.row]
        cell.configureCell(products: prods)
        cell.productCart.tag = indexPath.row
        
    } else if collectionView == self.manPerfumeCV {
        let prods = manPerfumeData[indexPath.row]
        cell.configureCell(products: prods)
        cell.productCart.tag = indexPath.row
        
    } else if collectionView == self.kidsPerfumeCV {
        let prods = kidsPerfumeData[indexPath.row]
        cell.configureCell(products: prods)
        cell.productCart.tag = indexPath.row
        
    }

and in the same view controller I have this action for the button from the xib file

@IBAction func iPhoneAddToCart(_ sender: AnyObject) {
        let butt = sender as! UIButton
        let indexP = IndexPath(row: butt.tag, section: 0)
        let cell = manPerfumeCV.cellForItem(at: indexP) as! iPhoneCollectionViewCell
        
        print(manPerfumeData[indexP.row].price)
        
    }

Each collection view has its own [Products] array.

1) manPerfumeData
2) womanPerfumeData
3) kidPerfumeData.

In my code if I tap at the button which is at the 1st collection view with manPerfumeData it prints the price very well.

Although if I push the button on the 2nd or 3rd collection view the app crashes.

Is there any way to know from which collection view they pushed the button so I can load the specific [Products] array?

Thanks!


Solution

  • You can use tag property of UIButton. Let consider your code

    let cell:iPhoneCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "iPhoneCell", for: indexPath) as! iPhoneCollectionViewCell
    if collectionView == self.womanPerfumeCV {
        //Your Code
        cell.productCart.tag = indexPath.row + 1000
    
    } else if collectionView == self.manPerfumeCV {
        //Your Code
        cell.productCart.tag = indexPath.row + 2000
    
    } else if collectionView == self.kidsPerfumeCV {
        //Your Code
        cell.productCart.tag = indexPath.row + 3000
    }
    

    You noticed that I changed the tag property of UIButton. Now when tap on button, check tag property of UIButton. Like this

    if (sender.tag >= 1000 && sender.tag<2000)
    {
      //manPerfumeCV
    }
    else if (sender.tag >= 2000 && sender.tag<3000)
    {
      //womanPerfumeCV
    }
    else
    {
      //kidsPerfumeCV
    }
    

    For fetching value from array, subtract the beginning value from tag property and you will get value, like this

    For manPerfumeCV

    indexValue = sender.tag - 1000
    

    For womanPerfumeCV

    indexValue = sender.tag - 2000
    

    For kidsPerfumeCV

    indexValue = sender.tag - 3000
    

    This value can directly use for getting data from array.