弹回后刷新时UICollectionView消失

时间:2018-06-18 08:15:07

标签: ios swift uicollectionview

使用UICollectionView进行快速刷新时遇到问题。刷新控件运行良好,但是当我单击一个单元格时,按下另一个视图控制器,然后弹回,尝试刷新,UICollectionView消失(我不使用故事板)。

屏幕录制视频: https://www.youtube.com/watch?v=3bsaqqv6x_8

这是我的代码:

的AccountController

class AccountController: UICollectionViewController, UICollectionViewDelegateFlowLayout{

    var menuData: [Any]? {
        didSet{
            self.collectionView?.reloadData()
        }
    }

    let collectionLayout = UICollectionViewFlowLayout()
    let refreshControl = UIRefreshControl()

    override func viewDidLoad() {
        ...
        setupCollectionView()

        self.collectionView?.isHidden = false
        refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
        collectionView!.addSubview(refreshControl)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        super.collectionViewLayout.invalidateLayout()
    }
    func setupCollectionView(){
        self.collectionView?.isHidden = true
        collectionView?.backgroundColor = UIColor(white: 0.95, alpha: 1)
        collectionView?.alwaysBounceVertical = true
        collectionView?.register(MenuCell.self, forCellWithReuseIdentifier: cellId)
        collectionView?.register(MenuSupplementary.self, forCellWithReuseIdentifier: supplementaryId)

        collectionLayout.minimumLineSpacing = 0
        collectionView?.collectionViewLayout = collectionLayout
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if let count = menuData?.count {
            return count
        }
        return 0
    }


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

        if let dt = menuData?[indexPath.item] {
            if dt is MenuCellData{
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MenuCell
                cell.data = dt as? MenuCellData
                return cell
            }

            if dt is MenuSupplementaryData {
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: supplementaryId, for: indexPath) as! MenuSupplementary
                cell.data = dt as? MenuSupplementaryData

                return cell
            }
        }

        return UICollectionViewCell()
    }
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: view.frame.width, height: 45)
    }

    @objc func refresh(sender:UIRefreshControl){
        let api = AccountApi(jwt: appDelegate.userDefault.string(forKey: "jwt"))
        api.getMenuData { (data) in
            var dt: [Any] = []
            if data.isEmpty { return }
            for d in data as! [MenuData] {
                if d.type == "supplementary" {
                    dt.append(MenuSupplementaryData( d.text ))
                }

                if d.type == "cell" {
                    dt.append(MenuCellData(d.icon!, d.text, isTheLastItem: d.isTheLastItem ?? false, url: d.url, httpMethod: d.httpMethod ))
                }
            }
            if !dt.isEmpty {
                self.menuData = dt
            }
            sender.endRefreshing()
        }
    }
}

TabBarController

class TabBarController: UITabBarController, UITabBarControllerDelegate {
        ...
        private func setUpNavControllerFrom(viewController: UIViewController, title: String, imageName: String)-> UINavigationController{
        let navController = UINavigationController(rootViewController: viewController)
        navController.tabBarItem.title = title
        navController.tabBarItem.image = UIImage(named: imageName)

        let imageView = UIImageView.init(frame: CGRect())

        imageView.image = UIImage(named: "logo")
        imageView.contentMode = .scaleAspectFit
        imageView.heightAnchor.constraint(equalToConstant: 20).isActive = true // Set logo height
        viewController.navigationItem.titleView = imageView
        navController.navigationBar.barTintColor = themeColor

        return navController
    }

    //   Set up color, logo, font ...
    private func setupViewControllers(){

        tabBar.tintColor = UIColor.white
        tabBar.barTintColor = themeColor

        let accountVC = AccountController(collectionViewLayout: UICollectionViewFlowLayout())
        let accNavController = setUpNavControllerFrom(viewController: accountVC, title: "Account", imageName: "account-icon")

        ...
    }
}

其他信息:

  • 刷新而不回弹:调用numberOfItemsInSection时会调用委托的cellForItemAtsizeForItemAtcollectionView?.reloadData()
  • 但是,在回弹后刷新时,调用cellForItemAt时不会调用sizeForItemAtcollectionView?.reloadData()代理。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

感谢您帮助我!

我终于发现了我的错误。 当选择处理菜单时,我创建了当前collectionViewLayout的新控制器,我认为当回弹时,当前布局会发生变化,make菜单会消失。

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let dt = menuData?[indexPath.item]{
            if dt is MenuCellData {
                let d = dt as! MenuCellData
                // let controller = AccountMenuController(collectionViewLayout: collectionView.collectionViewLayout)
                let controller = AccountMenuController(collectionViewLayout: UICollectionViewLayout())
                controller.menuData = d

                navigationController?.pushViewController(controller, animated: true)
            }
        }
    }
相关问题