0 Replies - 690 Views - Last Post: 16 May 2016 - 01:30 AM

#1 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 199
  • Joined: 18-May 11

How to trigger UICollectionView's reload method from another funct

Posted 16 May 2016 - 01:30 AM

I have a UICollection view which loads just fine when I launch the app. My problem is, I need to reload it with fresh data when a button from the menubar is clicked. Here's my code. To keep things simple, I've greatly trimmed it down

        class RootViewController:  UIViewController,UICollectionViewDelegateFlowLayout, UICollectionViewDataSource{//The view controller that contains the collection view
            var carouselCollectionView: UICollectionView!//A reference to the collectionview
            override func viewDidLoad() {
                super.viewDidLoad()

                let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
                layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
                layout.itemSize = CGSize(width: carouselWindowWidth, height: carouselWindowHeight)
                layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
                layout.minimumInteritemSpacing = 5
                layout.minimumLineSpacing = 5
        
                carouselCollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
                carouselCollectionView.frame = CGRectMake(0,0,200,200)
                carouselCollectionView.delegate = self
                carouselCollectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
                carouselCollectionView.backgroundColor = UIColor.clearColor()

                self.view.addSubview(carouselCollectionView) //Add the collectionview to the main view(self)
            }//End viewDidLoad


            //Delegates for collectionViewController go here
            func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                return currentImageArray.count
            }


            func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
                let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
                /*...*/
             }

             func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
                 /*...*/
             }


             //This function is triggered when a menuItem on the menuBar is clicked.  It it supposed to fetch some data from some source, then reload the collectionview
            func reloadData(VC:UIViewController){
                /*...*/
                carouselCollectionView.reloadData()
            }

        }//End RootViewController





I get this error when "reloadData()" is run "fatal error: unexpectedly found nil while unwrapping an Optional value". Any ideas on how to do this correctly? Any help greatly appreciated
PS. The data as expected when I run reloadData() within viewDidLoad()

Is This A Good Question/Topic? 0
  • +

Page 1 of 1