Loading and displaying random cells in UICollectionView

823 Views Asked by At

Pre-requisites - Environment: iOS 9.0 or above - using Swift 3.0.1

Thanks for your responses. I'm updating the question and trying to give a better understanding about the problem.

Posting code would help may be but I'm not allowed to post the code as I do not have the IP.

But I am trying to build something like calendar/program guide where you have events for each category for several days.

Imagine, categories on your left side in a column and they can be the sections of the collectionveiw and each category has events for several days which is a row.

CAT 1 : Event 1, Event 2 ... Event n CAT 2 : Event 1, Event 2 ... Event n CAT 3 : Event 1, Event 2 ... Event n . . . CAT m : Event 1, Event 2 ... Event n

Problem: The entire data is pretty dynamic and humongous. I can't prefetch all the records, they are about over 80-100K. It takes few minutes to download all the data and display it on the grid.

A user could select any day and any time and I have to scroll the collection view to that day and time and display those events for the categories. Also, user could obviously scroll in both directions to and browse the events in this case the events are loaded like infinite scroll fashion.

In the former option though, when the user jumps on to a particular day and time on the entire timeline and I have to skip loading the other previous events (as I do not have them yet - unknown) and display the events relevant to the user selected days and time.

I do not have all the IndexPaths in advance, to display on the screen, how can I skip events and dynamically update the collection view in parts like we load images dynamically and the ones which get loaded first and displayed earlier than others.

I'm using startDate of the events to calculate the xPosition, categories don't change often after they are loaded so we could somehow avoid reloading sections but items in those sections change all the time and they appear in a random fashion.

When the controller loads the first set of events are fetched from the server and displayed, now if the user decided to jump to some D-Day and T-Time which could be anywhere on the entire timeline I have to fetch the events for those dates and populate the items for relevant sections (visible on screen) and update the interface. This is where I have issues, where I do not have an proper approach.

Hope this is clearer. I have "tried" to mock this up

2

There are 2 best solutions below

1
eNeF On
0
Just a coder On

You have the same problem I had with my calendar project. The solution I have implemented will not work for you, but I am mentioning it here so that it might give you clues on how to solve it for your situation.

My calendar has a function where a user can scroll to some date way into the future. The problem was that date cells can be custom sizes. Therefore, since they are scrolling to some future date, in order for me to know the destination offset, I needed to know the offsets of cells 0 -to- destinationOffset because the cell sizes are different. This meant I had to query the sizes of all the cells in the middle which led to a 2-3 lag time (or in your case, a long download time).

So here was my solution. I originally had a delegate function called sizeForCellsAtMonth which was called for every month in order to determine the size. I have now changed this function to be called only once.

The function now only has two parameters:

  1. defaultSizeOfCells
  2. exceptionToDefaults - this will be specific months where the cell sizes are different

Using this information, I can calculate the sizes of all months because I know the sizes before hand. So my problem was solved by changing the way I looked at my delegate. Maybe you can try looking somewhere along those lines or maybe my answer gave you clues of what you can do.