ios基础篇(二十八)—— UITableView的上拉加载
本文主要展示一个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的上拉加载的更多相关文章
- ios基础篇(十八)——Delegate 、NSNotification 和 KVO用法及其区别
一.Delegate Delegate本质是一种程序设计模型,iOS中使用Delegate主要用于两个页面之间的数据传递.iphone中常用@protocol和delegate的机制来实现接口的功能. ...
- iOS开发之--iPhone X 适配:MJRefresh上拉加载适配
问题如下图: 出现原因,phoneX系列手机下方多了34像素的工作区域,所以需要对x全系列手机坐下适配, 解决如下: self.tableView.mj_footer.ignoredScrollVie ...
- RecyclerView下拉刷新上拉加载(二)
listview下拉刷新上拉加载扩展(一) http://blog.csdn.net/baiyuliang2013/article/details/50252561 listview下拉刷新上拉加载扩 ...
- C#构造方法(函数) C#方法重载 C#字段和属性 MUI实现上拉加载和下拉刷新 SVN常用功能介绍(二) SVN常用功能介绍(一) ASP.NET常用内置对象之——Server sql server——子查询 C#接口 字符串的本质 AJAX原生JavaScript写法
C#构造方法(函数) 一.概括 1.通常创建一个对象的方法如图: 通过 Student tom = new Student(); 创建tom对象,这种创建实例的形式被称为构造方法. 简述:用来初 ...
- IOS tableview下拉刷新上拉加载分页
http://code4app.com/ios/快速集成下拉上拉刷新/52326ce26803fabc46000000 刷新没用用插件,加载使用的MJ老师的插件. - (void)viewDidLoa ...
- UITableView与UISearchController搜索及上拉加载,下拉刷新
#import "ViewController.h" #import "TuanGouModel.h" #import "TuanGouTableVi ...
- iOS MJRefresh下拉刷新(上拉加载)使用详解
下拉刷新控件目前比较火的有好几种,本人用过MJRefresh 和 SVPullToRefresh,相对而言,前者比后者可定制化.拓展新都更高一点. 因此本文着重讲一下MJRefresh的简单用法. 导 ...
- iOS开发 XML解析和下拉刷新,上拉加载更多
iOS开发 XML解析和下拉刷新,上拉加载更多 1.XML格式 <?xml version="1.0" encoding="utf-8" ?> 表示 ...
- listview下拉刷新上拉加载扩展(二)-仿美团外卖
经过前几篇的listview下拉刷新上拉加载讲解,相信你对其实现机制有了一个深刻的认识了吧,那么这篇文章我们来实现一个高级的listview下拉刷新上拉加载-仿新版美团外卖的袋鼠动画: 项目结构: 是 ...
随机推荐
- 关于Linq中的Lambda表达式中OrderBy的深入理解
起因:就是一段Linq语句,OrderBy里面的i是什么? IQueryable<Student> slist = (from s in EFDB.Student select s). O ...
- 亚马逊EC2弹性IP收费
刚收到亚马逊云账单,被扣了4.44刀,吓尿,麻溜的去查账单详情,发现之前弃用的美国实例有个弹性IP没释放掉,占着茅坑还不拉屎,被扣钱宝宝无话可说,赶紧释放... $0.005 per Elastic ...
- Excel 数据关联
=INDEX(Sheet2!$A$2:$A$77,MATCH(A4,Sheet2!$C$2:$C$77,0))
- What technical details should a programmer of a web application consider before making the site public?
What things should a programmer implementing the technical details of a web application consider bef ...
- ionic入门之色彩、图标、边距和界面组件:列表
目录: 色彩.图标和边距 色彩 图标 内边距 界面组件:列表 列表:.list 成员容器:.item .item: 嵌入文本 .item : 嵌入图标 .item : 嵌入头像 .item : 嵌入缩 ...
- spring.net学习(一) 搭建环境,实例化spring.net容器。
1,使用nuget安装SPring.Core.安装这个的同时,会把Common.Logging,Common.Logging.Core也装上. 2,建立对象配置xml文件.如图Objects.xml. ...
- 在SQL SERVER中获取表中的第二条数据
在SQL SERVER中获取表中的第二条数据, 思路:先根据时间逆排序取出前2条数据作为一个临时表,再按顺时排序在临时表中取出第一条数据 sql语句如下: select top 1 * from(se ...
- [poj2349]Arctic Network(最小生成树+贪心)
Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17758 Accepted: 5646 D ...
- 把数据输出到Word (组件形式)
上一篇的文章中我们介绍了在不使用第三方组件的方式,多种数据输出出到 word的方式,最后我们也提到了不使用组件的弊端,就是复杂的word我们要提前设置模板.编码不易控制.循环输出数据更是难以控制.接下 ...
- NTFS系统的ADS交换数据流
VC++ 基于NTFS的数据流创建与检测 What are Alternate Streams?(交换数据流) NTFS alternate streams , 或者叫streams,或者叫ADS(w ...