Question

i have a Collection view. I need to call web service when ever user swipes the collection view and reload the whole collection table from the web service response. Is This possible and a good approach. and if so can u tell me how to implement this.

Was it helpful?

Solution

Is This possible

Yes, it's definitely possible.

a good approach

Probably not. Network requests can have high latency, which means that you could end up with a delay between the swipe and the moment when the user sees new data. You'd probably have a better user experience if you loaded the new data before the user swipes, so that you'll have it ready to go with no delay.

how to implement this

Start by reading up on collection views. It's not feasible to explain everything you need to know here in one little answer, and Apple has already written an entire guide about it.

It's not clear from your question whether you mean for the swipe to be in the same direction that the collection view scrolls. Let's consider the two cases, then:

Swipe in same direction as scrolling: You don't need to do any work here to make the collection scroll -- collections are scroll views. You should have your collection view's data source load as much data as is reasonable. When the collection requests cells for which data isn't available (or when the collection view is merely close to needing cells for which data isn't available), the data source should request more data from the web service.

Swipe at right angle to scrolling direction: E.g. the collection view scrolls horizontally, and the swipe is vertical. You'd use a setup like this when you want the user to be able to swipe between different sets of data with a gesture. Use a gesture recognizer to detect the swipe, and use the gesture's action to tell the data source to switch data sets. Then tell the collection to reload its cells. You can use -[UICollectionView reloadData] method here, but you may also want to change the scroll position with -[UICollectionView scrollToItemAtIndexPath:atScrollPosition:animated:].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top