Search code examples
iosswiftfunctionasynchronousviewcontroller

How to init collectionView after loading custom function


I have a problem with func collectionView inside in class MoviesViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UIScrollViewDelegate {

This function init automatically, and I don't want to start automatically. Or to do so when it has finished loading a function that load external data.

This is my structure code of ViewController.swift

class MyViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UIScrollViewDelegate {
  @IBOutlet var scrollView : UIScrollView!
  @IBOutlet var collectionView1 : UICollectionView!

This is function i want load before CollectionView

func loaddata(){
  let url: NSURL = NSURL(string: "http://www.mywebsite/testdata.php")!
  let request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
  let bodyData = "data=something"
  request.HTTPMethod = "POST"

  request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
  NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
  (response, data, error) in
    let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)!               
  }
}

override func viewDidLoad() {
  super.viewDidLoad()
  loaddata()
}

override func viewDidLayoutSubviews() {
  self.scrollView!.contentSize = CGSizeMake(1920, 2200)
}

And here, func CollectionView init automatically

//this is function that load automatically
func collectionView(collectionView: UICollectionView,
  layout collectionViewLayout: UICollectionViewLayout,
  insetForSectionAtIndex section: Int) -> UIEdgeInsets {
  return UIEdgeInsets(top: 0.0, left: 50.0, bottom: 0.0, right: 50.0)
}

// this is function load automatically
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
  if (collectionView == self.collectionView1) {
    let cell : FeaturedCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierFeatured, forIndexPath: indexPath) as! FeaturedCollectionViewCell
    let  imageFilename = "featured-\(indexPath.row).jpg"
    cell.featuredImage.image = UIImage(named: imageFilename)
    return cell
  }
  return UICollectionViewCell()
}

How I can do to keep ViewController start before loadData? Or to ViewController not start until you call the function from loadData


Solution

  • You shouldn't care about collection view loading items when you don't have the items yet. The worst case that can happen is that nothing is displayed.

    However, when your data finishes loading, you should call collectionView.reloadData to display the data you have just finished loading from the server.

    If you don't want the collection view to be displayed at all when the data is still being loaded, simply set collectionView.hidden = true and thencollectionView.hidden = false in your completion handler. You should probably also display an activity indicator (e.g. UIActivityIndicator instance) to inform the user that data is still being loaded.

    Also I would advise not to load data from viewDidLoad. You should rather use viewDidAppear.