skip to Main Content

I’m making an app that allows a user to "pin" certain elements in a collection view I have implemented in my home ViewController class. To pin an element, the user must access a button that is part of my WordCell (UICollectionViewCell) class. However, when I try to press the button from my home view controller, nothing happens.

Here is all the relevant code and screenshots:

The star on the right hand side is the button inside the CollectionViewCell that I want the user to be able to push through the home view.

Below is all the relevant code in my ViewController class. I am using a delegate to pass the cell that was pressed into my home class ViewController. I also plan on passing more data back and forth between the cell (UICollectionCellView) class and ViewController in the future.

extension ViewController: UICollectionViewDataSource{
//......
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let wordCell = collectionView.dequeueReusableCell(withReuseIdentifier: wordCellReuseID, for: indexPath) as! WordCell
        
        wordCell.delegate = self
        
        wordCell.configure(word: tempWords[indexPath.item])
        return wordCell
    }
//......
}

extension ViewController: WordCellDelegate{
    func star(wasPressedOnCell: WordCell){
        print("touched")
        
        if(wasPressedOnCell.isStarred){ //if the button has already been starred, unstar it
            wasPressedOnCell.starButton.setImage(UIImage(named: "unfilled_star.png"), for: .normal)
            wasPressedOnCell.isStarred = false
        }
        
        else{ //else, star the button
            wasPressedOnCell.starButton.setImage(UIImage(named: "filled_star.png"), for: .normal)
            wasPressedOnCell.isStarred = true
        }
    }
}

Here is relevant code in my class that conforms to UICollectionCellView:

//delegate setup for home class
protocol WordCellDelegate: class{
    func star(wasPressedOnCell cell: WordCell) //parameter: cell that was pressed
}

//........

//button setup
starButton = UIButton()
        starButton.setImage(UIImage(named: "unfilled_star.png"), for: .normal)
        starButton.translatesAutoresizingMaskIntoConstraints = false
        starButton.addTarget(self, action: #selector(starred), for: .touchUpInside)
        contentView.addSubview(starButton)

//......

//button objective function
@objc func starred(){
        print("touched")
        
        delegate?.star(wasPressedOnCell: self)
        
        //starredTapAction?() //chained back to main view controller
    }

However, when I try to press the star on my home view controller screen, the objc function inside my UICollectionCellView class is not called. I’ve read from previous posts that this is most likely due to a hierarchy of classes and which view controls which objects, but I haven’t been able to find a solution to this issue yet. I’m not sure what needs to be changed so the button inside the collection cell can be pressed through the view of the collection.

Please let me know if you need any more information, and thank you for reading this post!

2

Answers


  1. Please disable cell selection of UIcollection view and your button touch event will fire, basically at a time you can either use collection view did select method or button action method

    Login or Signup to reply.
  2. I had the exact same issue less than 2 weeks ago; I am still not sure about the root cause but I know the fix.

    Move this line —
    starButton.addTarget(self, action: #selector(starred), for: .touchUpInside)

    inside your cell configuration method —
    WordCell.configure (word: )

    Add —
    starButton.isEnabled = true
    starButton.isUserInteractionEnabled = true
    under your —
    //button setup
    only if the tap is not registering (ie if you can’t see the button getting tapped)

    It’s really about setting the button target under cellForItemAt(); that’ll do the trick.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search