UICollectionView的页脚没有重新加载

时间:2018-05-23 13:29:59

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout

我在按下UICollectionView按钮时显示并隐藏UICollectionViewCell的页脚视图。

要显示UICollectionView的页脚视图,我在x中将高度设为collectionView(_:layout :referenceSizeForFooterInSection),并将同一方法中的传递高度隐藏为0

用于检测是否需要显示或隐藏footerView我在同一方法中获取UICollectionViewCell并根据需要进行检查。

但这里是我遇到问题的事件的步骤。

  1. 扩展FooterView
  2. 按设备的“主页”按钮
  3. 再次从最近的应用程序转到应用程序
  4. 现在我没有将FooterView视为Expanded。
  5. 我做了一些调试并且知道在回到应用程序后我无法获取UICollectionViewCell它返回nil。到this answer我发现,如果UICollectionViewCell不可见,那么它将返回nil。所以我在1之后通过调用self.collectionView?.collectionViewLayout.invalidateLayout()重新加载了FooterView 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()
            }
        }
    

    请帮我解决这个问题。

0 个答案:

没有答案
相关问题