views:

271

answers:

2

I have a horizontal UIScrollView that contains UIButtons (though it could be any object). I would like to only download images for these buttons as the user scrolls and they are seen. I would also like an activityindicator running on each button while the images are downloading. Here's what I've tried with results:

  1. Check content offset of uiscrollview and download images for visible buttons. Problem: issues getting activity view shown within subclassed UIButton and not desired usability since images are only downloaded once scrolling stops.

  2. Add subviews (not buttons) to the UIScrollview hoping to use the view's ViewController to initiate a downloaded on viewDidAppear. Problem: viewDidAppear never seems to get called (because I am using addSubView?).

I've seen other apps that do this kind of loading but need some guidance on the typical set-up. Ebay's app is a great example. All items have a default image until the item comes into view, then an activityindicator runs and the items unique image is downloaded.

Thanks for assistance.

A: 

Most such apps are using a UITableView, not a plain scroll view. Joe Hewitt's Three20 framework has a TTImageView class that can do lazy image loading and is pretty well suited to this kind of use.

Noah Witherspoon
Unfortunately the design goal requires a horizontal scroll but I'll look into the three20 framework you mention. Thanks.
Greypoint
It may be possible to programmatically rotate/warp a UITableView so that it's going sideways and you could use the same code. The Apple top paid apps example also shows how to lazily load images.
Cirrostratus
A: 

Without actually trying it out, here's my idea:

You can use the UIScrollViewDelegate method scrollViewDidScroll: to find out when your scroll view finished scrolling and then use [UIScrollView contentOffset] to determine which part of the scroll view is visible and (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view to determine the button's position within the visible part of the scroll view. Have a method that checks if the button has had its image loaded yet and if not, load it and refresh the view if necessary.

Cirrostratus