views:

281

answers:

2

Tell me if I am wrong with that:

I made an new class ScrollViewDataSource. That class conforms to an protocol I created within my delegate for the scroll view. Well, it's a very special delegate that does some very complex stuff upon scrolling. So this delegate receives that data source object upon initialization. The datasource object now has a method

  • (NSArray*)subviewsFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex;

so when the user scrolls, the delegate rings the datasource object lots of times dunring scrolling to ask for data. I'm going to recycle views during scrolling. That means, if the user scrolls down, I remove the views from the top to the bottom, and fill them up with new data.

The NSArray contains UIView objects, which will be positioned appropriately during scrolling. Also, on first launch of the view that contains the scroll view, the data source will deliver the data to display the first visible contents.

Is that a good pattern, or do you have better ideas for that? BTW: I know a UITableView does something similar. But I want to learn that. It's a practise for me. Thanks!

+1  A: 

May,

This is the best pattern that you can make use of. Exclusively followed by Apple in various of their data displaying views like UITableView in iPhone and NSTableView,NSOutlineView in Mac.

All the best.

Shreekara
A: 

If your content views are using same layout, I would make ScrollViewDataSource containing only the data that content views needed, so I don't need to alloc/create new UIView for my UIScrollView when asking new data (since MyScrollView hold the content views that I can reuse):

@interface MyScrollView : UIView {
@private
    id <MyScrollViewDelegate> _delegate;
    id <MyScrollViewDataSource> _dataSource;

    UIScrollView *_scrollView;
    NSMutableArray *_contentViews; // you need to create/maintain/reuse contentView from here
}

@property (nonatomic, assign) id <MyScrollViewDelegate> delegate;
@property (nonatomic, assign) id <MyScrollViewDataSource> dataSource;
@end

@protocol MyScrollViewDataSource <NSObject>
@optional
- (NSString *)myScrollView:(MyScrollView *)myScrollView requestTitleForContentViewAtIndex:(NSInteger)index;
- (UIImage *)myScrollView:(MyScrollView *)myScrollView requestLogoForContentViewAtIndex:(NSInteger)index;
@end

...

The good about this is, your MyScrollView interface would look clean to superview, and you are dealing all the scrolling, layout, redraw and content updating stuffs within your MyScrollView AND without bothering other views or controllers from outside.

But, if your content views are totally different to each others, I won't use pattern like this.

digdog