2017-01-30 36 views
3

我有一個具有多個集合視圖的視圖控制器。具有相同XIB單元格和按鈕的多個集合視圖

每個集合視圖與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 

    } 

,並在同一個視圖控制器我從廈門國際銀行文件,這個動作按鈕

@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] ar射線。

1) manPerfumeData 
2) womanPerfumeData 
3) kidPerfumeData. 

在,如果我輕按按鈕,在與manPerfumeData一號集合視圖它打印的價格非常好我的代碼。

雖然如果我按下第二個或第三個集合視圖上的按鈕應用程序崩潰。

有沒有什麼辦法從極地收集視圖知道他推動按鈕,所以我可以加載spesific [產品]數組??

謝謝!

+0

你怎麼在調試器輸出看到加選擇? –

+0

@SinaKH致命錯誤:意外發現零,同時展開一個可選值 2017-01-30 14:25:38.928171 Aromania [19538:6823926]致命錯誤:意外地發現零,同時展開一個可選值 因爲在'print'我只有'manPerfumeData'。 如何識別按鈕按下哪個集合視圖? –

回答

3

您可以使用UIButtontag財產。 讓考慮你的代碼

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財產。現在點擊按鈕時,檢查tag財產UIButton。像這樣

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 

對於womanPerfumeCV

indexValue = sender.tag - 2000 

對於kidsPerfumeCV

indexValue = sender.tag - 3000 

該值可以從陣列獲取數據直接使用。

+0

好吧,我會嘗試。 –

+0

是它的正確!我想出了最後一次更新,你必須使' - 1000'或反正我在每個標籤上添加的數字!非常感謝你的配偶:D –

2

序來實現它,你可以在viewDidLoad中

override func viewDidLoad() { 

// your code 

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

} 

使用此您可以識別源

//現在就聽聽buttonEvent

標籤屬性添加到collectionViews

我建議將事件監聽器保持在同一個控制器中,或者僅使用索引和標記進行協議回調。
爲了簡單起見,您作出相同的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); 

    } 
} 
相關問題