I use UICollectionView
+ AFNetworking 2
for loading images asynchronously from a webservice. It's working great. However, how do I implement automatic scrolling (when user reaches bottom of page, or close to it, the API is called to fetch more cells).
I've seen lots of examples but there doesn't seem to be a best practice. For example, one tutorial suggested scrollViewDidScroll
, but that's called every time the view is scrolled. Is that the correct implementation?
PS. I'm coding in Swift.
Here's my cell creation code:
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as UICollectionViewCell
let offer = self.offers[indexPath.row]
var cellImageView = cell.viewWithTag(100) as UIImageView
let requestURL = NSURLRequest(URL: offer.largeImageURL)
cellImageView.setImageWithURLRequest(
requestURL,
placeholderImage:placeholderImage,
success: {
(request:NSURLRequest!, response:NSHTTPURLResponse!, image:UIImage!) in
cellImageView.image = image
},
failure: {
(request:NSURLRequest!, response:NSHTTPURLResponse!, error:NSError!) in
NSLog("GET Image Error: " + error.localizedDescription)
}
)
return cell
}