本文主要展示一个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){,,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 ;
} //返回每个分组中的行数
- (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){,,,}];
NSString *str = [dataArray objectAtIndex:indexPath.row];
title.text = str;
title.font = [UIFont systemFontOfSize:];
title.textColor = [UIColor blueColor];
[cell addSubview:title]; return cell;
} //设置TableViewCell行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return ;
} //开始加载
- (void)loadDataBegin{
if (loadMore == NO) { loadMore = YES;
//设置滚动轮
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:(CGRect){,,,}];
[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 = ; 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){,,self.view.frame.size.width,}];
footerView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:]; UILabel *title = [[UILabel alloc] initWithFrame:(CGRect){,,self.view.frame.size.width,}];
title.text = @"上拉显示更多数据";
title.font = [UIFont systemFontOfSize:];
title.textAlignment = NSTextAlignmentCenter;
[footerView addSubview:title]; tableView.tableFooterView = footerView; }

ios基础篇(二十八)—— UITableView的上拉加载的更多相关文章

  1. ios基础篇(十八)——Delegate 、NSNotification 和 KVO用法及其区别

    一.Delegate Delegate本质是一种程序设计模型,iOS中使用Delegate主要用于两个页面之间的数据传递.iphone中常用@protocol和delegate的机制来实现接口的功能. ...

  2. iOS开发之--iPhone X 适配:MJRefresh上拉加载适配

    问题如下图: 出现原因,phoneX系列手机下方多了34像素的工作区域,所以需要对x全系列手机坐下适配, 解决如下: self.tableView.mj_footer.ignoredScrollVie ...

  3. RecyclerView下拉刷新上拉加载(二)

    listview下拉刷新上拉加载扩展(一) http://blog.csdn.net/baiyuliang2013/article/details/50252561 listview下拉刷新上拉加载扩 ...

  4. C#构造方法(函数) C#方法重载 C#字段和属性 MUI实现上拉加载和下拉刷新 SVN常用功能介绍(二) SVN常用功能介绍(一) ASP.NET常用内置对象之——Server sql server——子查询 C#接口 字符串的本质 AJAX原生JavaScript写法

    C#构造方法(函数)   一.概括 1.通常创建一个对象的方法如图: 通过  Student tom = new Student(); 创建tom对象,这种创建实例的形式被称为构造方法. 简述:用来初 ...

  5. IOS tableview下拉刷新上拉加载分页

    http://code4app.com/ios/快速集成下拉上拉刷新/52326ce26803fabc46000000 刷新没用用插件,加载使用的MJ老师的插件. - (void)viewDidLoa ...

  6. UITableView与UISearchController搜索及上拉加载,下拉刷新

    #import "ViewController.h" #import "TuanGouModel.h" #import "TuanGouTableVi ...

  7. iOS MJRefresh下拉刷新(上拉加载)使用详解

    下拉刷新控件目前比较火的有好几种,本人用过MJRefresh 和 SVPullToRefresh,相对而言,前者比后者可定制化.拓展新都更高一点. 因此本文着重讲一下MJRefresh的简单用法. 导 ...

  8. iOS开发 XML解析和下拉刷新,上拉加载更多

    iOS开发 XML解析和下拉刷新,上拉加载更多 1.XML格式 <?xml version="1.0" encoding="utf-8" ?> 表示 ...

  9. listview下拉刷新上拉加载扩展(二)-仿美团外卖

    经过前几篇的listview下拉刷新上拉加载讲解,相信你对其实现机制有了一个深刻的认识了吧,那么这篇文章我们来实现一个高级的listview下拉刷新上拉加载-仿新版美团外卖的袋鼠动画: 项目结构: 是 ...

随机推荐

  1. vs快捷键大全

    前言 作为一个.net开发员,你还在用鼠标去点击相应的操作么?如果你回答是,那么你太low了! 一个很厉害的程序员不会是那种这鼠标到处狂点的人,他们肯定会很多快捷键,所以为了离他们更近一步,我们必须学 ...

  2. 刚知道的android属性

    在EditText中当设置的高度是wrap_parent,但是随着我们输入的越来越多,编辑框会被拉伸的很丑,所以就用了maxLines属性,设置maxLines="2"说明最多输入 ...

  3. sendEmail报错:at /usr/share/perl5/vendor_perl/IO/Socket/SSL.pm

    sendEmail发送邮件是出现以下报错: *******************************************************************  Using the ...

  4. [OC][地图] 高德地图之定位初探(一)

    使用前的说明 高德地图开放平台的iOS定位模块网址-->http://lbs.amap.com/api/ios-location-sdk/summary/ 高德地图有Web端.android平台 ...

  5. 扩展卡尔曼滤波(MRPT)

    EKF relies on a linearisation of the evolution and observation functions which are good approximatio ...

  6. Makefile中头文件在依赖关系中作用

    摘于:http://bbs.csdn.net/topics/120024677 (1)在makefile的依赖关系中用不用体现.h头文件?(2)如果在依赖关系中要体现.h头文件,应该体现到什么层次?= ...

  7. DataTable转换为Json字符串的三种方法

    //第一种:使用StringBuilder  public string DataTableToJson(DataTable table) { var JsonString = new StringB ...

  8. 采用dom4j和反射模拟Spring框架的依赖注入功能

    Spring的依赖注入是指将对象的创建权交给Spring框架,将对象所依赖的属性注入进来的行为.在学习了dom4j后,其实也可以利用dom4j和反射做一个小Demo模拟Spring框架的这种功能.下面 ...

  9. (十一)socket、connect、bind函数详解

    一.socket函数 1.头文件: #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> 2.函数原型: ...

  10. centos7 安装redis 开机启动

    redis 下载 https://redis.io/download wget http://download.redis.io/releases/redis-3.2.6.tar.gz 解压缩 .ta ...