如何从目标函数集(Swift 3)访问UICollectionView中的按钮

问题描述 投票:1回答:4

在我的View Controller中,我有一个集合视图,在渲染时会显示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”功能中的其他按钮吗?

ios uicollectionview swift3 xcode8
4个回答
0
投票

有多种方法可以解决这个问题。 UICollectionView视图有一个方法visibleCells()返回它的可见单元格的数组。您可以使用它来获取指向您的单元格的指针。您需要一种方法来确定哪一个是哪个。例如,您可以使用indexPath(for: UICollectionViewCell)来计算每个单元格的索引路径。


0
投票

我不知道这是否有帮助,但如果将indexPath存储在cellForItemAt方法的结构上呢?

你将会有:

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

}

0
投票

它可能会迟到但对Swift 4版本的某些人仍然有用:你可以使用发送者superview作为UiCollectionViewCell *考虑集合视图单元格中的发送者层次结构

func selectedSwatch(sender: UIButton) {
   let cell = sender.superview?.superview as! trimSelectionCell
   //cell.yourbtn 
}

0
投票

试试这个,

 class trimSelectorVC: UIViewController, UICollectionViewDelegate, 
 UICollectionViewDataSource {

@IBOutlet weak var trimSelector: UICollectionView!

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

var trimArray: [trimObject] = []

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

    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)
   if(trimArray[indexPath.item].isSelected == "0"){
   // button not clicked
  // change shadow color of button

    }
   else
    {
   // button  clicked
       cell.trimButton.layer.borderWidth = 2
      cell.trimButton.layer.borderColor = UIColor(red: 83/255, green: 
       71/255,blue: 65/255, alpha: 1.00).cgColor
    }
     // set tag to the button
     cell.trimButton.tag = indexPath.item 

    //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.

  let index = sender.tag
   for obj in trimArray {
   obj.isSelected = "0"
   }

   trimArray[index].isSelected = "1"

  collectionView.reloadData()
}

}

© www.soinside.com 2019 - 2024. All rights reserved.