0

I need to create a custom layout based on http call to an api. Based on result from api in loop i will populate an array from CollectionView

In CustomLayout I need to use this array in order to draw correctly details in each row. In collectionView I use reload data after the data was added to array

Thank YOu

code:

class epgViewController:  UIViewController,  UICollectionViewDelegate, UICollectionViewDataSource{

    var events     = [Event]()  
      @IBOutlet weak var collectionView: UICollectionView!

      //another initializations
      . . .

       //====== load Events ===============
        func get_events() {

            //http api call
            //as result in loop i append data to Events()
              self.events.append(newEvent)
        //reload collection view data    
        self.collectionView.reloadData()
       }
    }
//////////
/// LAyout
////////
     class customCollectionViewLayout: UICollectionViewLayout {

     //prepare layout  
     override func prepareLayout() {
        for section in 0...collectionView!.numberOfSections()-1 {
            how to access array from collectionView
            Events[i] ????

      }
     }  
    }
  • Please review Stack Overflows article on [ask]. Welcome to Stack Overflow. – Dan Beaulieu Oct 23 '15 at 14:38
  • I'm not sure I understand your issue... because I don't see where your _model_ layer is, which could serve even your custom layout as your view controller as well. – holex Oct 23 '15 at 14:49
  • In your question can you explain clearly, 1.) what is happening in your code now 2.) what you expect to happen in your code. Please be very explicit and someone will have an answer for you. i can't figure out what you're doing based on what you've provided. – Dan Beaulieu Oct 23 '15 at 16:59
  • hello.This in not full code !This is just logic.1) In collection view i have an array Events(). While user open view, in background is created http call to api. Data are received, events are append to array.2 Based on Events I need to create a grid. 3. From begining i don't now how many rows will be created in collection view.Due to this after call I reload collection view data. prepareLayouts() is called. In this method i need to access Events() array from collection view to create cells. The questin is : how to use Events() from CollectionView in Layout ? Or this is imposible ? – Igor Malasevschi Oct 23 '15 at 18:30
  • How to get data from one view controller to another is a very frequently asked question. Please take a careful look at the duplicate question and answers -- even though the language used there is Objective-C rather than Swift, I think you'll find that it completely explains what you need to do. – Caleb Oct 23 '15 at 18:50
  • 1
    Hello. It seems you don't understood my question. I don't need to pass data between 2 controllers. This is one controller with Collecton view. I need to pass data between Collection view and Layout.Please be more careful of the code above. – Igor Malasevschi Oct 23 '15 at 18:54
  • @Caleb while I am sure there are many questions on this particular topic. This question is not a duplicate of the link you provided, by any means. The link you provided is passing data while changing views. This is passing data from different code bodies in the same view. – Dan Beaulieu Oct 23 '15 at 19:00

1 Answers1

0

I'm not entirely sure I understand your situation, but I'll provide two methods for communication within your application.

Using NSNotificationCenter

Add an observer In the viewDidLoad of the controller that needs an event fired:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "remoteRefresh:", name:"remoteRefreshID", object: nil)

In that same controller add an observer with a name the same as our selector, in the above example "remoteRefresh".

func remoteRefresh(notification: NSNotification) {
    // do stuff
}

Then in your other controller can post messages and your remoteRefresh action will run:

NSNotificationCenter.defaultCenter().postNotificationName("remoteRefreshID", object: nil)

for more details I have a two part tutorial on NSNotificationCenter

Protocol / Delegate pattern

The second is the Protocol / Delegate pattern which is a bit more involved but I have a written tutorial here:

protocol / delegate pattern

Another Option

Here's an option if you're still having issues. You could declare a global struct to hold your data, paste this into a playground to understand.

struct SessionData {

    static var events = [String]()
}


class classOne {

    func addValues() {

        SessionData.events.append("a message")
        SessionData.events.append("another message")
    }
}


class classTwo {

    func getValues() -> [String] {

        return SessionData.events

    }

}

classOne().addValues()
classTwo().getValues()
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135