简体   繁体   中英

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 wich collection view he pushed the button so i can load the spesific [Products] array ??

Thanks!

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.

inorder to achieve it you can add tag property to the collectionViews in viewDidLoad

override func viewDidLoad() {

// your code

self.womanPerfumeCV.tag = 0;
self.manPerfumeCV.tag = 1;
self.kidsPerfumeCV = 2;

}

using this you can identify the source

// Now its time to listen to the buttonEvent

I would suggest to keep the event listener in the same controller or make a protocol callback only with index and tag.
To keep it simple lets make the changes in same viewController File

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

}

cell.yourButton.tag = (collectionView.tag * 1000 )+(indexPath.row);
        cell.yourButton.addTarget(self, action: #selector(ButtonClicked(_:)), forControlEvents: .TouchUpInside)


now add selector to event

func ButtonClicked(sender: UIButton) {
    let index : Int = sender.tag % 1000;
    switch sender.tag {
      case let x where x < 1000:
      loadDataFromFirstArrayWithIndex(index);break;
      case let x where x <2000:
      loadDataFromSecondArrayWithIndex(index);break;
      default:
      loadDataFromThirdArrayWithIndex(index);

    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM