Я показываю и скрываю нижний колонтитул UICollectionView
при нажатии кнопки UICollectionViewCell
.
Для отображения нижнего колонтитула UICollectionView
я передаю высоту как x
в collectionView(_:layout :referenceSizeForFooterInSection)
и для скрытия высоты прохода как 0
в том же методе.
для обнаружения, нужно ли мне показать или скрыть footerView, я извлекаю UICollectionViewCell
тем же методом и проверяю в соответствии с моими потребностями.
Но вот шаги события, где я встречаюсь с проблемой.
- Расширение FooterView
- Нажатие кнопки «Домой» на устройстве
- Снова перейдите кприложение из последних приложений
- Теперь я не получаю FooterView в расширенном виде.
Я сделал некоторую отладку и узнал, что после возвращения в приложение я не могучтобы получить UICollectionViewCell, он возвращал ноль.К этому ответу я узнал, что если UICollectionViewCell
не видно, то он вернет nil
.Поэтому я перезагружал FooterView, вызывая self.collectionView?.collectionViewLayout.invalidateLayout()
при отправке через 1 секунду в viewWillAppear
, но по-прежнему не получал результат при расширении FooterView.
Вот мой код
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()
}
}
Пожалуйста, помогитемне решить эту проблему.