2

I'm showing and hiding UICollectionView's footerView on button click of UICollectionViewCell.

For showing UICollectionView's footerView I'm passing height as x in collectionView(_:layout :referenceSizeForFooterInSection) and for hiding passing height as 0 in same method.

for detecting if I need to show or hide footerView I'm fetching UICollectionViewCell in the same method and checking as per my need.

But here are steps of event where I'm meeting problem.

  1. Expanding FooterView
  2. Pressing "Home" button of the device
  3. Again go to the app from recent applications
  4. Now I'm not getting FooterView as Expanded.

I did some debugging and came to know that After getting back to the app I'm not able to fetch UICollectionViewCell it was returning nil. By this answer I came to know that if UICollectionViewCell is not visible then it will return nil.
So I did reloading FooterView by calling self.collectionView?.collectionViewLayout.invalidateLayout() in dispatch after 1 second in viewWillAppear but still not getting the result as FooterView expanded.

Here is my code

override func viewWillAppear(_ animated: Bool) {
        var isAnyExpanded = false
        ...
        //Checking if any I need to display footerView of not
        ...
        if isAnyExpanded {
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                self.collectionView?.collectionViewLayout.invalidateLayout()
            }
        }
    }
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        ...
        //other conditions
        ...
        else if messages.count > 0 {
            let message = messages[0]
            if message.message_type == .hotel {
                var cellHeight:CGFloat = 0
                if let hotels = arrHotel[message.msg] {
                    let outerIndexPath = IndexPath(item: 0, section: section)
                    if let hotelCell = collectionView.cellForItem(at: outerIndexPath) as? HotelParentCollectionViewCell { //This is where getting nil after coming back from recent app
                        let visibleIndexPaths = hotelCell.subCollectionView.indexPathsForVisibleItems
                        if visibleIndexPaths.count > 0 {
                            let firstIndexPath = visibleIndexPaths[0]
                            let hotel = hotels[firstIndexPath.item]
                            if hotel.isExpanded
                            {
                                //Calculation for height
                            }
                        }
                    }
                }
                return CGSize(width: 0, height: cellHeight)
            }
            ...
            //other conditions
            ...
        }
        return CGSize(width: 0, height: 0)
    }

@objc func btnHotelDetailSelect(sender: UIButton) {
        var section = 0
        if sender.tag > 1000 {
            section = sender.tag / 1000
        }
        let item = sender.tag - (section * 1000)
        let message = arrMessageTmp[section][item]
        if let hotels = arrHotel[message.msg] {
            let hotel = hotels[item]
            if hotel.isExpanded {
                hotel.isExpanded = false
            }
            else {
                for hotels in arrHotel.values {
                    let expandedHotels = hotels.filter({$0.isExpanded})
                    for hotel in expandedHotels {
                        hotel.isExpanded = false
                    }
                }
                hotel.isExpanded = true
            }
            collectionView?.collectionViewLayout.invalidateLayout()
        }
    }

Please help me to solve this issue.

Mrugesh Tank
  • 3,495
  • 2
  • 29
  • 59

0 Answers0