2

I am trying to stop a UIActivityIndicator from animating when the visible cells have all been loaded. The indicator starts when I make a call to a remote server and I am currently stopping it right before return cell in:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

This is not perfect though because there are times where this method does not get called so the activityIndicator does not get stopped.

I have looked through the class reference for UICollectionView and haven't been able to come up with a solution.

The accepted answer to this post is what I am looking for but I can't seem to tweak it to work for a collection view.

How to detect the end of loading of UITableView

Any thoughts on how I might stop the activity indicator at the appropriate time?

Community
  • 1
  • 1
Ben
  • 967
  • 3
  • 9
  • 23
  • http://stackoverflow.com/questions/18857167/uicollectionview-cell-selection – Sport Mar 26 '14 at 05:06
  • @Sport, thanks for the quick response but I'm not sure how that is going to help me. The question and answer both deal `cellForItemAtIndexPath:`. I am already stopping the indicator there but as stated there are instances where the method isn't called even though the indicator has been started so it never stops. – Ben Mar 26 '14 at 05:15
  • Well this may not be the exact answer,But seems pointing out. The better approch to manage a case like this is handling in the collection view cell itself.Means an activity for a certain cell if you are loading content in collection cell level.So as the cell is loaded user can interact and start working on it rather than waiting for the whole section to get loaded. If all the content is recieved in the same network call.Make the network call and get the response with an indicator save the data and populate it after all the content is recieved – Lithu T.V Mar 26 '14 at 05:23
  • Is the activity indicator running while some background thread is loading data? Why not just disable it when you feed that data into your collection view? There should be no appreciable delay between `reloadData` and seeing the collection view contents on screen. – Christopher Pickslay Mar 27 '14 at 18:16
  • @ChristopherPickslay, The indicator is started when I begin the call to a remote server. The indicator continues to run while the retrieved data is mapped and persisted into core data entities, and finally it continues through the collection view data source and delegate methods. I believe what you are suggesting is that I disable the indicator when the collection view data source methods begin... Am I correct...? seems like a good solution. – Ben Mar 27 '14 at 20:59
  • @Ben yes. Generally you would invoke `reloadData` on the collection view after the retrieved data is stored, which causes the datasource and delegate methods to be invoked. At that point, when you call `reloadData`, remove the indicator. – Christopher Pickslay Mar 28 '14 at 03:39
  • @ChristopherPickslay, That is the route I have gone with and it works without flaw... If you want to throw your comments in a solution I will accept it! Thanks for your help. – Ben Mar 29 '14 at 01:39

1 Answers1

2

Assuming the activity indicator is running while some background thread is loading data, you should just disable it when you feed that data into your collection view.

Generally you would invoke reloadData on the collection view after the retrieved data is stored, which causes the datasource and delegate methods to be invoked. At that point, when you call reloadData, remove the indicator.

Christopher Pickslay
  • 17,523
  • 6
  • 79
  • 92
  • As stated by Christopher Pickslay, the delay was minimal upon reloading the data so I simply eliminated the indicator once I had successfully retrieved the data from the server and mapped it to the core data entities. – Ben Mar 31 '14 at 01:14