2016-11-27 22 views
1

在我的View Controller中,我有一個集合視圖,在呈現時顯示3個單元格,每個單元格都有一個標籤和一個按鈕。標籤顯示顏色的名稱,並且該按鈕具有顯示色樣的背景圖像。如何從目標函數集中訪問UICollectionView中的按鈕(Swift 3)

我希望它能夠讓您每當點擊其中一個按鈕時,該按鈕的周圍會有一個黑色的邊框,而其他按鈕上會有一個淺色邊框,以指示點擊按鈕被「選中」 。或者,我可以通過根據圖像的選定狀態更改圖像來做到這一點 - 但我的問題依然如此。

如何訪問其他兩個按鈕來切換其屬性?

我有一個腳本實現,允許我添加一個邊框到某人點擊的按鈕上 - 但我無法弄清楚如何訪問其他單元的CollectionView中的其他按鈕,以將其邊框屬性更改爲好。

下面是我的源代碼(與不相干/無關位剝離出來)

class trimSelectorVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 

    @IBOutlet weak var trimSelector: UICollectionView! 

    struct trimObject { 
     var trimName: String 
     var trimButton: String 
     var trimID: Int 
    } 

    var trimArray: [trimObject] = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     trimArray.append(trimObject(trimName: "Chrome", trimButton: "chrome-swatch", trimID: 0)) 
     trimArray.append(trimObject(trimName: "Gold", trimButton: "gold-swatch", trimID: 1)) 
     trimArray.append(trimObject(trimName: "Gun Metal", trimButton: "gunmetal-swatch", trimID: 2)) 

     trimSelector.delegate = self 
     trimSelector.dataSource = self 
    } 


    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
    } 

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

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

     //Set the label text 
     cell.trimLabel.text = trimArray[indexPath.item].trimName 

     //Set the image for the button 
     cell.trimButton.setImage(UIImage(named: trimArray[indexPath.item].trimButton), for: UIControlState.normal) 

     //Sets a target function for the button 
     cell.trimButton.addTarget(self, action: #selector(selectedSwatch), for: .touchUpInside) 

     return cell 
    } 


    func selectedSwatch(sender: UIButton) { 

     //These set the "selected" border to the button you clicked on. 
     sender.layer.borderWidth = 2 
     sender.layer.borderColor = UIColor(red: 83/255, green: 71/255, blue: 65/255, alpha: 1.00).cgColor 


    } 
} 

可有人請告訴我如何訪問我的「selectedSwatch」功能的其他按鈕?

回答

0

有多種方法可以處理這個問題。 A UICollectionView視圖有一個方法visibleCells(),該方法返回其可見單元格的數組。你可以用它來指向你的單元格。你需要一種方法來確定哪一個是哪個。例如,您可以使用indexPath(for: UICollectionViewCell)來計算每個單元格的索引路徑。

+0

我想這肯定是在正確的軌道上。我可以用這種方法拉取集合視圖中的單元格數量。我將如何使用返回的數組深入單元格內的UIButton? 喜歡的東西: VAR ALLCELLS = trimSelector.visibleCells() ALLCELLS [0] <<善有善報這裏>> layer.borderColor = UIColor.black –

+0

讓您的集合視圖細胞UICollectionViewCell的自定義子類。?。定義原型單元格,並在故事板中連接一個插座。然後將您從調用visibleCells返回的單元格轉換爲您的自定義單元格類型,然後使用插座查找按鈕。 –

+0

請參閱此鏈接瞭解設置細胞原型的解釋:http://www.techotopia.com/index.php/An_iPhone_iOS_6_Storyboard-based_Collection_View_Tutorial –

0

我不知道這是否會有所幫助,但如果您將IndexPath存儲在cellForItemAt方法的struct上,那該怎麼辦?

您將有:

struct trimObject { 
    var trimName: String 
    var trimButton: String 
    var trimID: Int 
    var idx : IndexPath 
} 

然後在:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! trimSelectionCell 
      .... 

trimArray[indexPath.item].idx = indexPath 
    .... 
} 

而在你selectedSwatch方法:

func selectedSwatch(sender: UIButton) { 

    //These set the "selected" border to the button you clicked on. 
    sender.layer.borderWidth = 2 
    sender.layer.borderColor = UIColor(red: 83/255, green: 71/255, blue: 65/255, alpha: 1.00).cgColor 

    if let cell = (sender.superview as? UICollectionViewCell) { 

     //Cell with the button selected: 
     let idx = collectionView.indexPath(for: cell) 

     //array of the other objects: 
     let allOtherObjects = trimArray.filter { ($0 as! trimObject).idx != idx } 

     allOtherObject.forEach({ (trimObj) in 
      let cell = collection.cellForItem(at: trimObj.idx) 
      //Do whatever yo need to do... 
      //cell.trimButton.layer 
     }) 
    } 

} 
相關問題