簡體   English   中英

具有相同XIB單元格和按鈕的多個集合視圖

[英]Multiple Collection Views with same XIB Cell and button

我有一個具有多個集合視圖的視圖控制器。

每個集合視圖都使用與xib相同的自定義單元格。 在這個xib中,我有一個按鈕。

collectionViews名稱是

1) manPerfumeCV
2) womanPerfumeCV
3) kidsPerfumeCV

cellForItemAt內部,我具有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

    }

在同一個視圖控制器中,我對xib文件中的按鈕執行此操作

@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)

    }

每個集合視圖都有自己的[Products]數組。

1) manPerfumeData
2) womanPerfumeData
3) kidPerfumeData.

在我的代碼中,如果我點擊帶有manPerfumeData的第一個集合視圖中的按鈕,它將打印出很好的價格。

盡管如果我按第二個或第三個收藏夾上的按鈕,應用程序將崩潰。

有什么辦法可以從收藏夾視圖中知道他按下了按鈕,以便我可以加載特殊的[Products]數組?

謝謝!

您可以使用UIButton tag屬性。 讓我們考慮一下您的代碼

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
}

您注意到我更改了UIButtontag屬性。 現在,當點擊按鈕時,檢查UIButton tag屬性。 像這樣

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

為了從數組中獲取值,請從tag屬性中減去起始值,您將獲得值,如下所示

對於manPerfumeCV

indexValue = sender.tag - 1000

對於女士香水

indexValue = sender.tag - 2000

對於孩子來說

indexValue = sender.tag - 3000

此值可直接用於從數組獲取數據。

為了實現它,您可以在viewDidLoad中將tag屬性添加到collectionViews中。

override func viewDidLoad() {

// your code

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

}

使用此您可以識別來源

//現在該聽按鈕事件了

我建議將事件偵聽器保留在同一控制器中,或者僅使用索引和標記進行協議回調。
為了簡單起見,讓我們在同一viewController文件中進行更改

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)


現在將選擇器添加到事件

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);

    } 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM