分页加载数据策略
来源:互联网 发布:java 领域模型 编辑:程序博客网 时间:2024/05/30 04:20
1.构建一个计数的抽象基类classsA,对需要加载不同的数据类型实例化一个对象classB。
2.抽象基类开放一下接口:
typedef enum { RequestStatusNone, RequestStatusInProgress, RequestStatusDone // request succeeded or failed} RequestStatus;@protocol NMPaginatorDelegate@required- (void)paginator:(id)paginator didReceiveResults:(NSArray *)results ifRefresh:(BOOL)ifRefresh;@optional- (void)paginatorDidFailToRespond:(id)paginator;- (void)paginatorDidReset:(id)paginator;@end@interface NMPaginator : NSObject {}@property (assign,nonatomic) id<NMPaginatorDelegate> delegate;@property (assign, readonly) NSInteger pageSize; // number of results per page@property (assign, nonatomic) NSInteger page; // number of pages already fetched@property (assign, readonly) NSInteger total; // total number of results@property (assign, readonly) RequestStatus requestStatus;@property (nonatomic, retain) NSMutableArray *results;- (id)initWithPageSize:(NSInteger)page_size delegate:(id<NMPaginatorDelegate>)paginatorDelegate;- (void)reset;- (BOOL)reachedLastPage;- (void)fetchFirstPage;- (void)fetchNextPage;// call these from subclass when you receive the results- (void)receivedResults:(NSMutableArray *)dataAry total:(NSInteger)totalPage ifRefresh:(BOOL)ifRefresh;- (void)failed;
抽象基类classA一个私有虚函数,方便子类重写,实现请求网络数据的功能
classA.m源文件中
#pragma mark - Sublclass methods- (void)fetchResultsWithPage:(NSInteger)page pageSize:(NSInteger)pageSize ifRefresh:(BOOL)ifRefresh{ // override this in subclass}
子类:classB.m中重写该函数
@implementation classB- (void)fetchResultsWithPage:(NSInteger)page pageSize:(NSInteger)pageSize ifRefresh:(BOOL)ifRefresh{ DataService *service = [DataService GetInstance]; [service getdate:xxx page:page pageSize:pageSize callBack:^(id service,NSMutableArray *arr,int totalNum){ [self receivedResults:arr total:totalNum ifRefresh:ifRefresh]; }];}
3.在初始化的时候启动计数调用fetchFirstPage接口获取第一页的数据
4.classB请求网络数据,得到数据传回classA,classA对数据进行处理
- (void)receivedResults:(NSMutableArray *)dataAry total:(NSInteger)totalPage ifRefresh:(BOOL)ifRefresh{ if (ifRefresh) { if (dataAry) { results = nil; results = dataAry; } }else{ if (dataAry) { NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithCapacity:10]; [tmpArray addObjectsFromArray:results]; [tmpArray addObjectsFromArray:dataAry]; results = nil; results = tmpArray; tmpArray = nil; } } page ++; total = totalPage; requestStatus = RequestStatusDone; if (self && self.delegate) { [self.delegate paginator:self didReceiveResults:results ifRefresh:ifRefresh]; }}
分装完成后用代理传回控制器
- (void)paginator:(id)paginator didReceiveResults:(NSArray *)results ifRefresh:(BOOL)ifRefresh;若没有获取到网络数据,建议加载本地数据库
3.在控制器中的scrollerview的代理中调用fetchNextPage接口
#pragma mark - UIScrollView Delegate- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGPoint offset = scrollView.contentOffset; CGRect bounds = scrollView.bounds; CGSize size = scrollView.contentSize; UIEdgeInsets inset = scrollView.contentInset; float reload_distance = 10; if(offset.y + bounds.size.height - inset.bottom > size.height + reload_distance && ifLoadMore) { ifLoadMore = NO; if(![classB reachedLastPage]) { // fetch next page of results [classB fetchNextPage]; } }}
0 0
- 分页加载数据策略
- 从服务端加载分页数据的策略
- ETL数据加载策略
- ListView分页加载数据
- ListView分页加载数据
- listView分页加载数据
- listView 分页加载数据
- 分页加载数据
- ListView分页加载数据
- ListView 分页加载数据
- listView分页加载数据
- AngularJs分页加载数据
- Android 分页加载数据
- xListView分页加载数据
- ListView分页加载数据
- ListView分页加载数据
- ListView分页加载数据
- ListView分页加载数据
- Spring 注解学习(六) 测试
- android 移动开发 手势切换页面实现动画效果
- Revit结构详图对于钢铁制造和架设过程
- Web for Pentester XSS Example 01-09
- Relization重要性运作分析在金融业
- 分页加载数据策略
- 如何更改Oracle 数据文件的位置
- Live555 实战之框架简介
- java笔记(五)
- 用户付费渗透率再研究
- linux在shell中获取时间
- Activity has leaked window that was originally added 问题解决
- 可积 连续 可微 可导关系
- HDU1065 I Think I Need a Houseboat 【数学递推】