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
我创建了一个新类ScrollViewDataSource。该类符合我在代理中为滚动视图创建的协议。嗯,这是一个非常特殊的委托,在滚动时会做一些非常复杂的事情。因此,该委托在初始化时接收该数据源对象。数据源对象现在有一个方法
- (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.
NSArray包含UIView对象,在滚动期间将对其进行适当定位。此外,在首次启动包含滚动视图的视图时,数据源将传递数据以显示第一个可见内容。
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!
这是一个好的模式,还是你有更好的想法?顺便说一句:我知道UITableView做了类似的事情。但我想了解这一点。这对我来说很实践。谢谢!
2 个解决方案
#1
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.
这是您可以使用的最佳模式。 Apple在其各种数据显示视图中独家紧随其后,如iPhone中的UITableView和Mac中的NSTutViewView中的NSTutView。
All the best.
祝一切顺利。
#2
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):
如果你的内容视图使用相同的布局,我会让ScrollViewDataSource只包含所需内容视图的数据,所以在询问新数据时我不需要为我的UIScrollView分配/创建新的UIView(因为MyScrollView保存了我的内容视图)可以重复使用):
@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.
关于这一点的好处是,你的MyScrollView界面对于superview来说看起来很干净,而且你正在处理MyScrollView中的所有滚动,布局,重绘和内容更新,而不必从外面打扰其他视图或控制器。
But, if your content views are totally different to each others, I won't use pattern like this.
但是,如果您的内容视图彼此完全不同,我将不会使用这样的模式。
#1
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.
这是您可以使用的最佳模式。 Apple在其各种数据显示视图中独家紧随其后,如iPhone中的UITableView和Mac中的NSTutViewView中的NSTutView。
All the best.
祝一切顺利。
#2
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):
如果你的内容视图使用相同的布局,我会让ScrollViewDataSource只包含所需内容视图的数据,所以在询问新数据时我不需要为我的UIScrollView分配/创建新的UIView(因为MyScrollView保存了我的内容视图)可以重复使用):
@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.
关于这一点的好处是,你的MyScrollView界面对于superview来说看起来很干净,而且你正在处理MyScrollView中的所有滚动,布局,重绘和内容更新,而不必从外面打扰其他视图或控制器。
But, if your content views are totally different to each others, I won't use pattern like this.
但是,如果您的内容视图彼此完全不同,我将不会使用这样的模式。