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下拉刷新上拉加载-仿新版美团外卖的袋鼠动画: 项目结构: 是 ...
随机推荐
- 20161013001 DataGridView 数据转 DataTable
DataTable dt2 = GetDgvToTable(Form_CY_ProjectRequirements_D); MessageBox.Show( dt2.Rows.Count.To ...
- mac终端命令大全介绍(转)
OSX 的文件系统 OSX 采用的Unix文件系统,所有文件都挂在跟目录 / 下面,所以不在要有Windows 下的盘符概念. 你在桌面上看到的硬盘都挂在 /Volumes 下. 比如接上个叫做 US ...
- .net面试题集锦
1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成 ...
- Python的SublimeText开发环境配置
1.完成Python的默认安装之后要设置环境变量,系统环境变量Path中加入Python的安装目录";C:/Python27" 2.配置Python编译环境,修改[C:\Progr ...
- Signlar
后台内部发送到指定客户端 Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<tvHub>().Clie ...
- centos7 docker redis
docker run --name=redistmp -ti centos /bin/bash yum -y install gcc tcl make cd /home wget http://dow ...
- fzu1036四塔问题(汉诺塔问题拓展)
#include<iostream> #include<cstdio> #include<cmath> using namespace std; ]; int ru ...
- django例子,question_text为中文时候报错
问题描述 UnicodeEncodeError at /admin/polls/question/3/ 'ascii' codec can't encode characters in positio ...
- 绿色 或者 免安装 软件 PortableApps
Refer to http://portableapps.com/apps for detail. Below is just a list at Jan-01-2017 for quick show ...
- Filter过滤器的写法
http://pengenjing.iteye.com/blog/1607248 这里写的过滤器用的是适配器模式,思路为: 先写一个类实现Filter,然后在让你写的过滤器来继承自这个类: 步骤:1. ...