0

I have a stackView(called 'btnStack') which holds four buttons in a cell(class 'PresentationCell') which I am trying to hide with the following code:

func hideBtnStack() {
    let cell = collectionView.visibleCells.first as! PresentationCell

    cell.btnStack.isHidden = true

}

However, when I run the code I get the following error:

'Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value'

Is there an easier/better way to access the btnStack in the cell?

The relevant code in cellForRowAt is:

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

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

 }

I also have the btnStack declared in the PresentationCell class as follows:

class PresentationCell: UICollectionViewCell {

@IBOutlet weak var textView: UITextView!

@IBOutlet weak var btnStack: UIStackView!

}

And I first call the hideBtnStack method in the ViewDidLoad as follows:

override func viewDidLoad() {
    super.viewDidLoad()


    collectionView.dataSource = self
    collectionView.delegate = self

    setUpCollectionView()

    hideNavBar()

    hideBtnStack()


}

Thanks in advance for any help that you can provide.

Zenman C
  • 1,653
  • 13
  • 21
  • Are you sure the first visible cell is a `PresentationCell`? Can you share your `cellForRowAt` code? – Rakesha Shastri May 18 '19 at 03:54
  • How may types of cell do you have? – Amir May 18 '19 at 03:56
  • Thanks for the quick responses. CellforRowAt code has now been added above. In this presentationViewController there is only one type of cell which is the PresentationCell. – Zenman C May 18 '19 at 04:01
  • 1
    One possibility is that you call `hideBtnStack` before collection view has any data, can you also share where you set datasource and where you call `hideBtnStack` ? – Amir May 18 '19 at 04:04
  • Thanks. I originally thought this might be an issue as well. But I made sure the hideBtnStack was declared after the setUpCollectionView and relevant delegate and datasource methods in the viewDidLoad. – Zenman C May 18 '19 at 04:20
  • So, CjCoax gave me the clue on how to resolve this. It looks like if the hideBtnStack is declared in the viewDidLoad along with the methods for collectionView it executes before the collectionView can be populated. I resolved this by putting a delay around the hideBtnStack method and now it works. – Zenman C May 18 '19 at 04:45

2 Answers2

0

CjCoax gave me the clue on how to resolve this. It looks like if the hideBtnStack is declared in the viewDidLoad along with the methods for collectionView it executes before the collectionView can be populated. I resolved this by putting a delay around the hideBtnStack method and now it works.

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.dataSource = self
    collectionView.delegate = self


    setUpCollectionView()

    hideNavBar()

    DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
        self.hideBtnStack()
    }


}
Zenman C
  • 1,653
  • 13
  • 21
0

I think this solution may help you! try this one.

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

guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? PresentationCell else { return UICollectionViewCell() }

if indexPath.item == 0 { 
    // hide your view
    cell.btnStack.isHidden = true
}

}

NOTE: try to avoid from force unwrapping.

I hope this solution works for you.

  • Thanks for this code and to Rakesha's response. The reason though that I wrote the code in a separate function 'hideBtnStk' is that I wanted to be able to hide and unhide (i.e. toggle) the appearance of the UIStackView. I now know (I think) that the reason why the original function did not work is that it was called in the viewDidLoad just after the setting up of the collectionView. So, hideBtnStack does actually work. The problem now though, is that it only hides and unhides the buttons for the visible cell and not for the ones after (or previous) when scrolled to. – Zenman C May 20 '19 at 03:40
  • So, now I'm looking for a way to iterate over all the cells in the collectionView so that I can set the 'isHidden' status on the btnStack consistently. – Zenman C May 20 '19 at 03:44