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下拉刷新上拉加载-仿新版美团外卖的袋鼠动画: 项目结构: 是 ...
随机推荐
- 结构体struts的长度
在需要计算结构体大小的时候,涉及到的一个问题就是其对齐模数 计算机系统对基本类型数据在内存中存放的位置有限制,它们会要求这些数据的首地址的值是某个数k(通常它为4或8)的倍数,这就是所谓的内存对齐,而 ...
- android源码中,在系统多媒体数据库中增加一个字段
由于项目需求,在系统多媒体管理数据库里的存储图像文件的表中需要新增加一个字段,源码在:项目\packages\providers\MediaProvider\MediaProvider.java下,在 ...
- SpringMVC框架下的拦截器
在eclipse的javaEE环境下:导包.... web.xml文件中的配置: <?xml version="1.0" encoding="UTF-8" ...
- Nudnik Photographer -Ural1260动态规划
Time limit: 1.0 second Memory limit: 64 MB If two people were born one after another with one second ...
- [Spark] Spark的RDD编程
本篇博客中的操作都在 ./bin/pyspark 中执行. RDD,即弹性分布式数据集(Resilient Distributed Dataset),是Spark对数据的核心抽象.RDD是分布式元素的 ...
- [译]使用JMH进行微基准测试:不要猜,要测试!
英文原文:Micro Benchmarking with JMH: Measure, don't guess!翻译地址:使用JMH进行微基准测试:不要猜,要测试!原文作者:Antonio翻译作者:Ho ...
- [poj2182] Lost Cows (线段树)
线段树 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacula ...
- MySQL中int类型的字段使用like查询方法
方法参考自: http://stackoverflow.com/questions/8422455/performing-a-like-comparison-on-an-int-field 也就是使用 ...
- 前言,学习ios编程(坚持)
其实,尝尝有人很疑惑,不知道自己要干嘛,看到很多的培训机构,不知道怎么选择但是又想进入软件行业.其实呢学习不一定要靠培训机构,一定要培训,特别是 当人家把自己吹的天花乱坠的时候,然并卵.出来之后,也只 ...
- java内存泄漏的经典案例
这篇文章主要介绍了Java中典型的内存泄露问题和解决方法,典型的内存泄露例子是一个没有实现hasCode和 equals方法的Key类在HashMap中保存的情况,可以通过实现Key类的equals和 ...