本文主要展示一个demo实现UITableView的上拉加载数据;
先看看效果图:
接着上拉,加载更多数据:
主要实现的效果是在我们上拉结束拖拽之后,开始加载数据,数据加载的过程中有滚动轮提示用户正在加载,加载完成后滚动轮消失,加载的数据出现。直接看代码吧:
demo:
#import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{ UITableView *tableView; //数据数组 NSMutableArray *dataArray; //上拉时的加载数据 NSMutableArray *moreArray; //数据数量 NSUInteger dataNumber; //加载状态 BOOL loadMore; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CGFloat width = self.view.frame.size.width; CGFloat height = self.view.frame.size.height; //创建Table tableView = [[UITableView alloc] initWithFrame:(CGRect){0,20,width,height}]; tableView.dataSource = self; tableView.delegate = self; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.view addSubview:tableView]; [self readySource]; //创建底部表格 [self creatFooterView]; } //数据资源 - (void)readySource{ dataArray = [[NSMutableArray alloc] initWithObjects:@"数学", @"语文", @"英语", @"历史", @"地理", @"政治", @"物理", @"化学", @"生物", @"体育", @"音乐", @"美术", nil]; moreArray = [[NSMutableArray alloc] initWithObjects:@"Math", @"Chinese", @"English", @"History", nil]; } //返回分组个数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } //返回每个分组中的行数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //建立可重用标识符 static NSString *indentifier = @"UITableViewCell"; // NSString *indentifier = [NSString stringWithFormat:@"UITableViewCell%ld%ld",(long)indexPath.row,(long)indexPath.section]; UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:indentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier]; } //移除所有子视图 [cell.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { UIView *view = (UIView*)obj; [view removeFromSuperview]; }]; //添加新视图 UILabel *title = [[UILabel alloc] initWithFrame:(CGRect){20,10,200,30}]; NSString *str = [dataArray objectAtIndex:indexPath.row]; title.text = str; title.font = [UIFont systemFontOfSize:20]; title.textColor = [UIColor blueColor]; [cell addSubview:title]; return cell; } //设置TableViewCell行高 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 50; } //开始加载 - (void)loadDataBegin{ if (loadMore == NO) { loadMore = YES; //设置滚动轮 UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:(CGRect){150,30,30,30}]; [activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray]; [tableView.tableFooterView addSubview:activity]; [activity startAnimating]; [self loading]; } } //结束拖拽时调用得方法 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ //设置范围,下拉到最底部时开始加载数据 if (!loadMore &&scrollView.contentOffset.y>(scrollView.contentSize.height-self.view.frame.size.height)) { [self loadDataBegin]; } } //加载数据中 - (void)loading{ dataNumber = [dataArray count]; for (int i = 0; i < moreArray.count; i++) { [dataArray addObject:[moreArray objectAtIndex:i]]; } [tableView reloadData]; [self loadDataEnd]; } //结束加载 - (void)loadDataEnd{ loadMore = NO; //结束加载创建底部表格 [self creatFooterView]; } //创建底部表格 - (void)creatFooterView{ tableView.tableFooterView = nil; UIView *footerView = [[UIView alloc] initWithFrame:(CGRect){0,0,self.view.frame.size.width,50}]; footerView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1]; UILabel *title = [[UILabel alloc] initWithFrame:(CGRect){0,15,self.view.frame.size.width,20}]; title.text = @"上拉显示更多数据"; title.font = [UIFont systemFontOfSize:15]; title.textAlignment = NSTextAlignmentCenter; [footerView addSubview:title]; tableView.tableFooterView = footerView; }
ios基础篇(二十八)—— UITableView的上拉加载
原文:http://www.cnblogs.com/0320y/p/5105028.html