Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionViewCell gaining access to it's parent UICollectionViewController

I am having trouble with a UICollectionViewController, that registers a custom UICollectionViewCell as a header. Please see the attached picture.

Storyboard Screenshot

In the middle diagram, is a UICollectionViewController, and the red square is highlighting a UIImageView that is inside a UICollectionViewCell, registered as a header. I am trying to achieve the result of the right diagram, such that when the UIImageView of the header cell is pressed, a UIView with a list of information slides up from the bottom.

This is the structure of my code so far:

1) A UIView swift file that contains the list of information.

2) Instantiating the UIView and adding it to the subview within my UICollectionViewController.

Since the UIImageView(information circle icon) exists within the UICollectionViewCell, I have made it userInteractionEnabled, and added a TapGestureRecognizer to it. However, since I want to animate the UIView up from the UICollectionViewController, I am not sure how to access the UIView property within the UICollectionViewController from my UICollectionViewCell header file.

I know it sounds really confusing, but if someone could point me in the right direction, or possibly suggest re-structuring of my code, I would greatly appreciate it. If you would like more information, please let me know as well. Much thanks for your help.

Edit Update: Many thanks to John for the detailed explanation. At first glance, I thought the protocol would simply apply to the entire UICollectionViewCell, but glossed over the fact that it's the delegate?.clicked() within the the Gesture Recognizer that limits the scope. Your help is greatly appreciated John. Hope this helps others with the similar problems.

like image 495
iMoment Avatar asked Mar 10 '23 03:03

iMoment


1 Answers

In oder to get there you should implement a protocol on the UICollectionViewCell where you will tell the parent that the cell is tapped and what action it should take like this :

protocol MyCellDelegate: class {
    func clicked()
    func other()
}

class MyCell: UIColletionViewCell  {
    weak var delegate : MyCellDelegate?

    //call delegate?.clicked() where you have the gesture recogniser 

}

Then in cellForItemAtIndexPath or wherever you use MyCell write the following

 cell.delegate = self

Then implement the extension for your class:

 extension MyCollectionView: MyCellDelegate {
     func clicked() {
     //present the view here (the slide up)
     }
  }
like image 59
anho Avatar answered Apr 07 '23 07:04

anho