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下拉刷新上拉加载-仿新版美团外卖的袋鼠动画: 项目结构: 是 ...
随机推荐
- 类加载机制(深入理解JAVA虚拟机学习笔记)
1.类加载机制的定义 将class文件加载到内存,然后对class文件中的数据进行校验.解析和初始化,转换成可以被虚拟机直接使用的JAVA类型,这就是虚拟机的类加载机制.(在JAVA中,类的加载.连接 ...
- Word 宏命令大全
1. 为宏命令指定快捷键.在WORD中,操作可以通过菜单项或工具栏按钮实现,如果功能项有对应的快捷键的话,利用快捷键可以快速实现我们需要的功能.如最常见的CTRL+O.CTRL+A等等.WOR ...
- UVALive 3401 彩色立方体
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- Spark学习(二) -- Spark整体框架
标签(空格分隔): Spark 还记得上次的wordCount程序嘛?通过这个小程序,我们来一窥Spark的框架是什么样子的. sc.textFile("/usr/local/Cellar/ ...
- excel单元格内换行
强制换行:将光标置于拟换行处,按ALT+Enter键,即可强行换行.
- sublime text 3插件
Package Control Messages Emmet emmet插件 Thank you for installing Emmet -- a toolkit that can greatly ...
- 让PHP开发者事半功倍的十大技巧
如果你使用一面大镜子作为冲浪板会发生什么?或许你会在较短的时间内征服海浪,但是你肯定从内心深处明白,这不是冲浪的正确选择.同样的道理也适用于PHP编程,尽管这样的类比听起来有一些古怪.我们经常听到有人 ...
- Git_1基础操作,从安装到提交完成(windows)
github地址:https://github.com/zhangsai521314/Git 1:安装Git Bash(https://git-scm.com/),安装一路NEXT. 2:目录架构: ...
- iOS - AppRealTest App 真机测试
前言 1.准备 开发者账号 自从 Xcode7 出来之后,一般的真机测试不需要开发者账号,也就不需要看这篇教程,只有 app 具有 "推送" 等功能的时候,要真机测试就必须要开发者 ...
- vc6.0如何显示行号以及出现版本不兼容问题
有时编译时,提示某某行有错,但是要定位到某一行的话,如果在编辑页面能够将行号显示出来,查找也就更方便了,下面我来介绍一下让VC6.0显示行号的方法. 工具/原料 VC6.0.显示行号的插件 方 ...